diff --git a/docs/features/integrations/claude-code.md b/docs/features/integrations/claude-code.md index 87bcc03865..f3faf43cb9 100644 --- a/docs/features/integrations/claude-code.md +++ b/docs/features/integrations/claude-code.md @@ -695,7 +695,14 @@ file lifts the rule on its path, not the rule on definitions. A test counts when request asks for tests or the prompt names the test's file. For a request that asks for tests module-level code, and a unit of a source file the index could not parse, count too: a pytest fixture in `conftest.py` and a CI definition written as assignments are -module-level code outside any test directory. The hook reads whether a request asks for +module-level code outside any test directory. The same units, among them the lines of a +class outside its methods (the index chunks them as it chunks module-level code), count +as the first result of a [picked answer](#model-picked-pointers-for-a-bug-report). The +pick model's candidates hold such units, and it read the request against them before it +put that one first; a fix can be made outside any definition, in a setting or a class +attribute. A result below the first counts as in an answer with no pick, a definition +always and such a unit only for a request that asks for tests: the reply marks none of +them as a pick. The hook reads whether a request asks for tests as the search ranking does: from every search term (a word or a part of an identifier) of a request of at most 48 terms, and from the first 16 of a longer one, so an issue that names a test where the fault showed does not ask for tests. Whether a @@ -1083,7 +1090,7 @@ The wiring is `hooks/hooks.json`; `hooks/HOOKS.md` has per-hook notes. | Event | Script | Effect | Off switch | | ------------------------------------------------------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------- | | SessionStart | `loom-session-start.mjs` | one budgeted `context.pack` primer (working-memory note, profile, the session's memory stack, recent runs with `LOOM_CONTEXT_RUNS`; and the memories, decisions and procedures a search for the project finds or, where a new or cleared conversation's prompts get the recall, the pinned and newest ones, listed), one line naming an indexed checkout's code namespace and the repository's memory tag (left out in a new session whose MCP connection carries the checkout: the server then applies both), a line naming the project's saved session briefs, a task-inbox line (`task.counts`), a few lines naming the collections the token can read (`collection.list`), and a line when [the instruction block](#the-instruction-block) needs setup | `LOOM_INJECT_CONTEXT=false`, `LOOM_CODE_MAP=false`, `LOOM_TASK_INBOX_LINE=off`, `LOOM_COLLECTIONS_LINE=off`, `LOOM_SETUP_LINE=off` | -| UserPromptSubmit | `loom-prompt-pack.mjs` | no request with `prompt_pack` off; at its default no prompt text unless this Loom holds the checkout's own repository ([what a prompt gets](#what-a-prompt-gets)); otherwise up to three `code.search` pointers for the prompt, only when the index rates its answer low-risk or the pick worded it `high` or `medium` (with `LOOM_PROMPT_PACK_MIN_CONF` set, from that confidence instead) and then only the answer's places to edit, one pointer per place: a definition, a unit of a file the hook does not hold to definitions, a document or a test when the prompt names it or asks for documentation or tests, a Python migration or a release script only if named, and module-level code when it asks for tests; below them the test line for a pointer whose definition the prompt names ([the test gate](#the-test-gate)) | off unless `prompt_pack` or `LOOM_PROMPT_PACK=on`; `LOOM_PROMPT_CONTEXT=off` | +| UserPromptSubmit | `loom-prompt-pack.mjs` | no request with `prompt_pack` off; at its default no prompt text unless this Loom holds the checkout's own repository ([what a prompt gets](#what-a-prompt-gets)); otherwise up to three `code.search` pointers for the prompt, only when the index rates its answer low-risk or the pick worded it `high` or `medium` (with `LOOM_PROMPT_PACK_MIN_CONF` set, from that confidence instead) and then only the answer's places to edit, one pointer per place: a definition, a unit of a file the hook does not hold to definitions, a document or a test when the prompt names it or asks for one, a Python migration or a release script only if named, and module-level code when it asks for tests or as a first pick; below them the test line for a pointer whose definition the prompt names ([the test gate](#the-test-gate)) | off unless `prompt_pack` or `LOOM_PROMPT_PACK=on`; `LOOM_PROMPT_CONTEXT=off` | | UserPromptSubmit | `loom-prompt-recall.mjs` | one bounded `context.pack` recall for the prompt (memories, decisions, procedures), injected only when memory does not abstain: whole records, and none of code as it was before a commit the checkout already has; of a record linked to a file of the checkout that the prompt does not name, one line: its key and the file | `LOOM_PROMPT_RECALL=first` (default) / `on` / `off`; `LOOM_PROMPT_CONTEXT=off` | | UserPromptSubmit | `loom-run.mjs` | opens the session's run (`run.begin`, `run_id = cc-`, goal = the first prompt); the prompt does not wait for it | `LOOM_RUN_LEDGER=off` | | PreToolUse (Read, Grep; Bash for a command that runs `gh`) | `loom-code-shunt.mjs` | the code shunt and the tracker shunt ([next section](#the-code-shunt)) | `LOOM_CODE_SHUNT=off`, `LOOM_TRACKER_SHUNT=off` | diff --git a/docs/guides/issue-code-pointers.md b/docs/guides/issue-code-pointers.md index 59c2e5cdb9..d61468ffca 100644 --- a/docs/guides/issue-code-pointers.md +++ b/docs/guides/issue-code-pointers.md @@ -218,6 +218,10 @@ What the plugins do and the SDKs do not: `Cause (a candidate a model wrote from the indexed code, not checked): `. An SDK's answer has no pick and so no sentence: its block ends with its last pointer. +- **Count a first pick outside the definitions as a place.** The first result of an + answer the pick model words `high` or `medium` is a place also when it is module-level + code or the lines of a class outside its methods. An SDK's answer has no pick, so the + rule for its first result is the rule for every result. - **The install's options.** The test line under the pointers reads the checkout, and `LOOM_PROMPT_PACK_MIN_CONF` changes the gate. The SDK method has the default gate and no test line. diff --git a/integrations/claude-code-plugin/.claude-plugin/plugin.json b/integrations/claude-code-plugin/.claude-plugin/plugin.json index 6521f856c7..57b9395717 100644 --- a/integrations/claude-code-plugin/.claude-plugin/plugin.json +++ b/integrations/claude-code-plugin/.claude-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "loom", "displayName": "ClickHouse Loom", - "version": "1.2.181", + "version": "1.2.183", "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" diff --git a/integrations/claude-code-plugin/README.md b/integrations/claude-code-plugin/README.md index ff55137140..5bb6ecb39d 100644 --- a/integrations/claude-code-plugin/README.md +++ b/integrations/claude-code-plugin/README.md @@ -285,7 +285,7 @@ the duplicate, run `claude mcp remove -s user`. | `PostToolUse` (Bash / PowerShell) | `loom-test-gate.mjs` | **A changed file's own test is named when the file changes.** After a shell command in a loom-indexed repo, asks `code.test_gate` about each file the command changed (a dirty path it names, written since the session began) and, for a file that has a test of its own, attaches its command and what the index knows of it: `Loom test for src/router.py: .venv/bin/python -m pytest -q -rf tests/test_router.py`, then `It is the one test file named for that file.` and what the checkout proves of the command. Once per file and once per test per session, on the tool result, so it costs no model request; an edit tool's call gets the same line from `loom-blast-radius.mjs`. A file with no such test, a runner the checkout does not configure, a comment-only change and a prompt that ruled a test run out get nothing, and a command that only reads (`grep`, `cat`, `sed -n`, `git diff`) starts no git process and asks nothing (**Tests at edit time**, below). `LOOM_TEST_GATE=off` disables; `LOOM_TEST_GATE_DEPTH` sets the hops (1–3). | | `PostToolUse`, `PostToolUseFailure` (Bash) | `loom-test-report.mjs` | **Why a test failed, on the run whose output was cut.** In a checkout that configures pytest, SessionStart adds its result-file option to `PYTEST_ADDOPTS` for the session's shell commands, so every pytest run writes a private result file. After a test command whose visible output names no failure's reason (`pytest ... \| tail -3`), the hook adds a digest of that file to the tool result: the totals, up to three groups of failures by message, each with its first test and where it was raised; at most 700 characters. No request and nothing stored (**The test report**, below). `LOOM_TEST_REPORT=off` disables. | | `UserPromptSubmit` | `loom-prompt-recall.mjs` | **Default `first`**: one bounded recall on the first qualifying prompt of a session, including empty or abstaining answers. Clear named code-only questions skip default recall without consuming the attempt; mixed memory requests still qualify. `LOOM_PROMPT_RECALL=on` (or the install option `prompt_recall`) for every prompt, `off` to disable; `LOOM_PROMPT_CONTEXT=off` disables it too ([what a prompt gets](#what-a-prompt-gets)). One `context.pack` per prompt restricted to `memories`, `decisions`, `procedures` with the prompt as the query; injects the records the session has not seen (each plane's under its tag; memories that answer alone under none), only when memory is not abstaining and `retrieval_confidence ≥ 0.5`, whole records within 1,600 chars (`LOOM_PROMPT_RECALL_MAX_CHARS`; one that does not fit is left out; a block's only record that the pack cut, or the first when none fits, is cut at its last whole sentence and names the call that reads it whole, the one the server's mark on the line names, or else the key to read it whole by). A record of code as it was before a commit the checkout's `HEAD` already has is left out, and a fix record whose commit it has is added as one line that names the commit. A record linked to a file of the checkout is added as one line, its key and that file, unless the prompt names the file or the definition, or asks memory: `loom-file-context` adds the record's own line after a local read of the file or before an edit of it. With `LOOM_FILE_CONTEXT=off`, or outside a checkout of a repository this Loom indexes, the prompt gets the record whole. The memory-side sibling of the prompt pack: active retrieval — the model gets what memory knows about the prompt without asking — done per prompt, never per tool turn. | -| `UserPromptSubmit` | `loom-prompt-pack.mjs` | Runs where the Loom the session talks to holds the checkout's own repository in its code index (`LOOM_PROMPT_PACK=off` stops it; [what a prompt gets](#what-a-prompt-gets)). One bounded lookup: `code.symbol`, `code.callers` or `code.impact` for an explicit named question, otherwise `code.search` (top 3, no bodies). The repository lookup and the answer share one bound (`LOOM_PROMPT_PACK_TIMEOUT_MS`, 2.5 s; a searched bug report asks for the server's pick and has 6 s): what comes later is skipped. Self-contained requests, local-only changes and explicit memory questions skip code retrieval. Search emits only where the index labels its own answer low-risk or the server's pick worded it `high` or `medium` (with `LOOM_PROMPT_PACK_MIN_CONF` set, from that confidence instead), without abstention, and sends only the answer's places to edit, one pointer per place: a definition, a unit of a file the hook does not hold to definitions, a document or a test when the prompt names it or asks for documentation or tests, a Python migration or a release script only if named, and module-level code when it asks for tests; its pointers are candidates, and nothing tells the model to stop searching; typed answers preserve ambiguity and static-analysis limits. Named implementation or behavior questions request a whole definition body within 600 server-budget tokens; location/signature requests stay pointer-only and oversized bodies stay elided. No fallback request follows an empty typed answer. Pointers are deduplicated within the current answer, so later prompts can receive changed evidence. Below the pointers of a search answer goes the test gate's line (**Tests at edit time**, below) when a pointer's definition is named in the prompt and its Python file has exactly one test file named after it, in a checkout that configures pytest; it adds no request and a session gets a test once. | +| `UserPromptSubmit` | `loom-prompt-pack.mjs` | Runs where the Loom the session talks to holds the checkout's own repository in its code index (`LOOM_PROMPT_PACK=off` stops it; [what a prompt gets](#what-a-prompt-gets)). One bounded lookup: `code.symbol`, `code.callers` or `code.impact` for an explicit named question, otherwise `code.search` (top 3, no bodies). The repository lookup and the answer share one bound (`LOOM_PROMPT_PACK_TIMEOUT_MS`, 2.5 s; a searched bug report asks for the server's pick and has 6 s): what comes later is skipped. Self-contained requests, local-only changes and explicit memory questions skip code retrieval. Search emits only where the index labels its own answer low-risk or the server's pick worded it `high` or `medium` (with `LOOM_PROMPT_PACK_MIN_CONF` set, from that confidence instead), without abstention, and sends only the answer's places to edit, one pointer per place: a definition, a unit of a file the hook does not hold to definitions, a document or a test when the prompt names it or asks for one, a Python migration or a release script only if named, and module-level code when it asks for tests or as a first pick; its pointers are candidates, and nothing tells the model to stop searching; typed answers preserve ambiguity and static-analysis limits. Named implementation or behavior questions request a whole definition body within 600 server-budget tokens; location/signature requests stay pointer-only and oversized bodies stay elided. No fallback request follows an empty typed answer. Pointers are deduplicated within the current answer, so later prompts can receive changed evidence. Below the pointers of a search answer goes the test gate's line (**Tests at edit time**, below) when a pointer's definition is named in the prompt and its Python file has exactly one test file named after it, in a checkout that configures pytest; it adds no request and a session gets a test once. | | `Stop` | `loom-test-gate.mjs` | **Off by default; with the `stop_test_gate` option (or `LOOM_TEST_GATE=stop`), holds a session that ran no test since its last code change.** A held stop costs the session at least one more model request, so it is yours to turn on. Then, with uncommitted changes **this session made** in a loom-indexed repo (the dirty paths the transcript shows an edit tool targeted or a shell command named, written since the session began, by the file's modification time; a tree left dirty before the session gates nothing, even when a command reads or runs its files), asks `code.test_gate` what the change obliges — the tests to run, in evidence order with a `run` command when the harness is derivable, and the touched definitions a coverage run measured unexecuted — and blocks the stop **once** per distinct change with a reason that names them. The second stop on the same change passes. Any test run since the last code change lets the stop pass, and nothing is asked (**Stops that pass**, below); with `LOOM_TEST_GATE=block` only a run of the named tests does (**Answered stops**, below). `LOOM_TEST_GATE=off` disables the gate at both events. | | `Stop` | `loom-validate-gate.mjs` | **Compile green before done**: collects the memory keys this session wrote from the transcript, asks `namespace.validate` for a report scoped to exactly those keys, and blocks the stop **once** per distinct key set with the error and warning findings that name them (a conflict you opened, a fact whose `valid_to` has passed, an attestation your edit made stale) and each check's remedy. `info` findings never block; the second stop on the same keys passes. `LOOM_VALIDATE_GATE=off` disables. | | `Stop` | `loom-namespace-health.mjs` | **The store tells you when the namespace is misused**: once per session, one `namespace.audit` over the last day (no samples) for the session's namespace, asked by the first prompt's detached process so that a stop does not wait for it (a stop asks in place only when that process died before its answer); when it carries a critical finding — exact-key reads dwarfing searches, double-encoded values, rows the embedder gave up on, leases in the memory plane — you get **one line** naming the codes and `/loom-optimize`, once per session, at the first stop that finds the verdict, as a block-once reason (the one channel a Stop hook has to the model). Healthy namespace, no token, slow or down server: nothing. The same summary rides `context.pack` as the `namespace_health` section at session start. `LOOM_NAMESPACE_HEALTH=off` disables. | diff --git a/integrations/claude-code-plugin/hooks/HOOKS.md b/integrations/claude-code-plugin/hooks/HOOKS.md index 33b4c5bd7c..b2e7e0abcd 100644 --- a/integrations/claude-code-plugin/hooks/HOOKS.md +++ b/integrations/claude-code-plugin/hooks/HOOKS.md @@ -897,7 +897,15 @@ in. asks for tests module-level code, and a unit of a source file the index could not parse, count too: a pytest fixture in `conftest.py` and a CI definition written as assignments are module-level code outside any test - directory. The hook reads whether a request asks for tests as the search + directory. The same units, among them the lines of a class outside its + methods (the index chunks them as it chunks module-level code), count as + the first result of a picked answer (**The listwise pick**, below). The pick + model's candidates hold such units, and it read the request against them + before it put that one first; a fix can be made outside any definition, in + a setting or a class attribute. A result below the first counts as in an + answer with no pick, a definition always and such a unit only for a request + that asks for tests: the reply marks none of them as a pick. The hook reads + whether a request asks for tests as the search ranking does: from every search term (a word or a part of an identifier) of a request of at most 48 terms, and from the first 16 of a longer one, so an issue that names a test where the fault showed does not ask for tests. diff --git a/integrations/claude-code-plugin/hooks/loom-prompt-pack.mjs b/integrations/claude-code-plugin/hooks/loom-prompt-pack.mjs index 7e9f391c78..684d45d476 100644 --- a/integrations/claude-code-plugin/hooks/loom-prompt-pack.mjs +++ b/integrations/claude-code-plugin/hooks/loom-prompt-pack.mjs @@ -21,7 +21,10 @@ // lib/prompt-pack.mjs). In Python, C and C++, TypeScript and JavaScript, Go, // Rust and Java source a definition passes, and leading comments, imports // and module-level code do not; a unit of any other file passes whatever its -// kind. A test or a file of a test data directory passes when the prompt asks +// kind. Module-level code passes as the first result of an answer the pick +// model worded `high` or `medium` (below): the model read the prompt against +// its candidates and put that unit first. A test or a file of a test data +// directory passes when the prompt asks // for tests or names its file, a document when the prompt asks for // documentation or names its file, and a numbered Python migration, a C or // C++ file in a directory below a directory named `external`, a top-level diff --git a/integrations/claude-code-plugin/lib/prompt-pack.mjs b/integrations/claude-code-plugin/lib/prompt-pack.mjs index bd4921b9f1..9b4646cd6b 100644 --- a/integrations/claude-code-plugin/lib/prompt-pack.mjs +++ b/integrations/claude-code-plugin/lib/prompt-pack.mjs @@ -418,9 +418,10 @@ export function accept(res, minConfidence = null, picked = false) { // too: the index chunks a pytest fixture and a CI definition written as // assignments as `other`, on paths (`conftest.py`, `ci/`) that are not test // paths, and its ranking applies no kind prior to such a query for that -// reason. A unit of any other file is that file's content and counts whatever -// its kind: a line window of a file the index reads as text, a notebook's -// cells, a component's template, a schema, a shell script, configuration. +// reason. So does the `other` a picked answer puts first (`isPlace`). A unit +// of any other file is that file's content and counts whatever its kind: a +// line window of a file the index reads as text, a notebook's cells, a +// component's template, a schema, a shell script, configuration. const DEFINITION_KINDS = new Set(["function", "class_skeleton", "symbol"]); const DEFINITION_SOURCE_RE = /\.(?:py|pyi|pyw|c|cc|cpp|cxx|h|hpp|hxx|ipp|inl|ts|tsx|mts|cts|js|jsx|mjs|cjs|go|rs|java)$/i; @@ -571,8 +572,17 @@ function namesFile(text, path) { * not made in (`NOT_SOURCE_RES`) only when `text` names it. In source the * hook holds to definitions a place is a definition, whatever names the * file: not a file's preamble and, unless the query asks for tests, not its - * module-level code. */ -function isPlace(unit, asks, text) { + * module-level code. + * + * `firstPick` says the unit is the first result of a picked answer. There + * `other` is a place as a definition is: module-level code, the lines of a + * class outside its methods, a window of a file the index could not parse. + * The rule on definitions is for an answer no model has read. The pick + * model's candidates hold such units, and it read the request against them + * before it put this one first; a fix can be made outside any definition, + * in a setting or a class attribute. The rules on the path hold for that + * unit, and a file's preamble is no place there either. */ +function isPlace(unit, asks, text, firstPick) { const path = String(unit.path || ""); if (!path) return false; const named = namesFile(text, path); @@ -582,7 +592,7 @@ function isPlace(unit, asks, text) { if (!named) return false; } else if (!named && ((matches(DOCUMENT_RES) && !asks.docs) || matches(NOT_SOURCE_RES))) return false; if (DEFINITION_KINDS.has(unit.kind) || !DEFINITION_SOURCE_RE.test(path)) return true; - return asks.tests && unit.kind === "other"; + return (asks.tests || firstPick) && unit.kind === "other"; } const firstLine = (unit) => Number(unit.start_line || 0); @@ -639,11 +649,13 @@ export function places(res, { query, text = query, cap = 3, picked = false } = { const asks = { tests: asksForTests(query), docs: asksForDocs(query) }; const prompt = String(text || ""); const results = (res?.results || []).slice(0, picked ? cap : undefined); - if (picked && results.length && !isPlace(results[0], asks, prompt)) return []; + // Of a picked answer the reply proves one unit a pick, the first: it marks none below it. + const place = (unit) => isPlace(unit, asks, prompt, picked && unit === results[0]); + if (picked && results.length && !place(results[0])) return []; const seen = new Set(); const ranked = results.filter((unit) => { const pointer = pointerOf(unit); - if (seen.has(pointer) || !isPlace(unit, asks, prompt)) return false; + if (seen.has(pointer) || !place(unit)) return false; seen.add(pointer); return true; }); diff --git a/integrations/claude-code-plugin/tests/prompt-pack-hook.test.mjs b/integrations/claude-code-plugin/tests/prompt-pack-hook.test.mjs index e4211e5055..085415a566 100644 --- a/integrations/claude-code-plugin/tests/prompt-pack-hook.test.mjs +++ b/integrations/claude-code-plugin/tests/prompt-pack-hook.test.mjs @@ -825,6 +825,17 @@ test("a bug report asks for the server's pick, with no switch, and is sent by th assert.deepEqual(await (await contractFixture(t, misplaced(pick))).ask(report), SILENT, pick); const unpicked = await contractFixture(t, misplaced("")); assert.match(context(await unpicked.ask(report)), /^[^\n]+\n- src\/parser2\.py:10-14 combine2\n/); + // A first pick that is module-level code is a place: the model read the candidates and put it first. + const setting = { path: "src/defaults.py", start_line: 305, end_line: 307, symbol: "", kind: "other" }; + const outside = (pick) => ({ + "code.search": { ...answer(pick)["code.search"], results: [setting, ...results.slice(1)] }, + }); + for (const pick of ["high", "medium"]) { + const block = context(await (await contractFixture(t, outside(pick))).ask(report)); + assert.match(block, /^[^\n]+\n- src\/defaults\.py:305-307\n- src\/parser2\.py:10-14 combine2\n/, pick); + } + const served = await contractFixture(t, outside("")); + assert.match(context(await served.ask(report)), /^[^\n]+\n- src\/parser2\.py:10-14 combine2\n/); }); test("a bug report waits for the pick's bound, a question for its own, and neither past it", async (t) => { diff --git a/integrations/claude-code-plugin/tests/prompt-pack.test.mjs b/integrations/claude-code-plugin/tests/prompt-pack.test.mjs index df98775d3a..fbcb19f623 100644 --- a/integrations/claude-code-plugin/tests/prompt-pack.test.mjs +++ b/integrations/claude-code-plugin/tests/prompt-pack.test.mjs @@ -363,6 +363,72 @@ test("a picked answer lists the first pick's file first", () => { assert.deepEqual(places({ results: [long, other, method] }, { query, picked: true }), [method, other]); }); +test("the first result of a picked answer is a place when it is source outside the definitions", () => { + const chunk = (kind, path, start, end, symbol = "") => ({ path, start_line: start, end_line: end, symbol, kind }); + const query = "uploaded files get the wrong permissions"; + const picks = (results, text = query) => places({ results }, { query, text, picked: true }); + const setting = chunk("other", "pkg/conf/defaults.py", 305, 307); + const method = chunk("function", "pkg/files/storage.py", 215, 217, "Storage::file_mode"); + // The pick model read the candidates and put module-level code first: it is sent, and the places below it. + assert.deepEqual(picks([setting, method]), [setting, method]); + for (const path of INTENT.definition_source_paths) + assert.deepEqual(picks([unit("other", path)]), [unit("other", path)], path); + // An answer no model ordered keeps the plain rule: for a request that asks for no test, definitions. + assert.deepEqual(places({ results: [setting, method] }, { query }), [method]); + // The first result only: below it the plain rule decides, and the same code is a place for a tests request alone. + const constant = chunk("other", "pkg/files/storage.py", 1, 9); + assert.deepEqual(picks([setting, constant, method]), [setting, method]); + assert.deepEqual(picks([method, setting]), [method]); + const fixture = "which pytest fixture builds the temporary database"; + assert.deepEqual(places({ results: [method, setting] }, { query: fixture, picked: true }), [method, setting]); + // Every other rule of the place test holds for the first result: its kind, and a path the prompt does not name. + const dropped = [ + chunk("file_header", "pkg/conf/defaults.py", 1, 4), + chunk("comment_block", "pkg/conf/defaults.py", 1, 4), + { path: "pkg/conf/defaults.py", start_line: 305, end_line: 307 }, + chunk("other", "", 305, 307), + chunk("other", "tests/test_defaults.py", 305, 307), + ...INTENT.not_source_paths.map((path) => chunk("other", path, 6, 47)), + ]; + for (const first of dropped) assert.deepEqual(picks([first, method]), [], `${first.kind} ${first.path}`); + // A prompt that names the file lifts the rule on its path, as for any place. + for (const path of ["setup.py", "tests/test_defaults.py"]) { + const named = chunk("other", path, 6, 47); + assert.deepEqual(picks([named, method], `${path} sets the wrong mode`), [named, method], path); + } + // It is a range as any place is: a definition inside it that the prompt names is the pointer. + const source = chunk("other", "pkg/core/function.py", 1053, 1227); + const skeleton = chunk("class_skeleton", "pkg/core/function.py", 1053, 1172, "Derivative"); + const create = chunk("function", "pkg/core/function.py", 1268, 1383, "Derivative::__new__"); + assert.deepEqual(picks([source, skeleton, create], "Derivative reports the wrong kind"), [skeleton, create]); + assert.deepEqual(picks([source, skeleton, create]), [source, create]); + + // By the model's word: `high` and `medium` send it, `low` nothing, and no word the answer under the plain rules. + const report = "why do uploaded files get the wrong permissions"; + const body = searchBody({ org: "o", namespace: "code-x" }, report, { token: "" }); + assert.equal(body.rerank, PICK); + const answer = (word) => ({ + abstain: false, + retrieval_risk: "low", + results: [setting, method], + ...(word === undefined ? {} : { pick_confidence: word }), + }); + for (const word of ["high", "medium"]) { + const block = pointerBlock(body, answer(word), report); + assert.deepEqual(block.pointers, [setting, method], word); + assert.match( + block.text, + /\n- pkg\/conf\/defaults\.py:305-307\n- pkg\/files\/storage\.py:215-217 Storage::file_mode$/, + word, + ); + // Where only the word admits the answer, the first pick alone is sent, and it is that unit. + assert.deepEqual(pointerBlock(body, { ...answer(word), retrieval_risk: "medium" }, report).pointers, [setting]); + } + assert.deepEqual(pointerBlock(body, answer("low"), report), { pointers: [], text: "" }); + for (const word of ["", undefined]) + assert.deepEqual(pointerBlock(body, answer(word), report).pointers, [method], String(word)); +}); + test("a range over the stated length is pointed at by its first line and its length", () => { assert.equal(LONG_RANGE_LINES, 200); const line = (unit) => renderPack({ results: [unit] }).text.split("\n")[1]; diff --git a/integrations/codex-plugin/.codex-plugin/plugin.json b/integrations/codex-plugin/.codex-plugin/plugin.json index 430a254889..b7f4c3e924 100644 --- a/integrations/codex-plugin/.codex-plugin/plugin.json +++ b/integrations/codex-plugin/.codex-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "loom", - "version": "1.2.181", + "version": "1.2.183", "description": "Shared long-term memory and code intelligence for Codex, 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" diff --git a/integrations/codex-plugin/hooks/loom-prompt-pack.mjs b/integrations/codex-plugin/hooks/loom-prompt-pack.mjs index 7e9f391c78..684d45d476 100644 --- a/integrations/codex-plugin/hooks/loom-prompt-pack.mjs +++ b/integrations/codex-plugin/hooks/loom-prompt-pack.mjs @@ -21,7 +21,10 @@ // lib/prompt-pack.mjs). In Python, C and C++, TypeScript and JavaScript, Go, // Rust and Java source a definition passes, and leading comments, imports // and module-level code do not; a unit of any other file passes whatever its -// kind. A test or a file of a test data directory passes when the prompt asks +// kind. Module-level code passes as the first result of an answer the pick +// model worded `high` or `medium` (below): the model read the prompt against +// its candidates and put that unit first. A test or a file of a test data +// directory passes when the prompt asks // for tests or names its file, a document when the prompt asks for // documentation or names its file, and a numbered Python migration, a C or // C++ file in a directory below a directory named `external`, a top-level diff --git a/integrations/codex-plugin/lib/prompt-pack.mjs b/integrations/codex-plugin/lib/prompt-pack.mjs index bd4921b9f1..9b4646cd6b 100644 --- a/integrations/codex-plugin/lib/prompt-pack.mjs +++ b/integrations/codex-plugin/lib/prompt-pack.mjs @@ -418,9 +418,10 @@ export function accept(res, minConfidence = null, picked = false) { // too: the index chunks a pytest fixture and a CI definition written as // assignments as `other`, on paths (`conftest.py`, `ci/`) that are not test // paths, and its ranking applies no kind prior to such a query for that -// reason. A unit of any other file is that file's content and counts whatever -// its kind: a line window of a file the index reads as text, a notebook's -// cells, a component's template, a schema, a shell script, configuration. +// reason. So does the `other` a picked answer puts first (`isPlace`). A unit +// of any other file is that file's content and counts whatever its kind: a +// line window of a file the index reads as text, a notebook's cells, a +// component's template, a schema, a shell script, configuration. const DEFINITION_KINDS = new Set(["function", "class_skeleton", "symbol"]); const DEFINITION_SOURCE_RE = /\.(?:py|pyi|pyw|c|cc|cpp|cxx|h|hpp|hxx|ipp|inl|ts|tsx|mts|cts|js|jsx|mjs|cjs|go|rs|java)$/i; @@ -571,8 +572,17 @@ function namesFile(text, path) { * not made in (`NOT_SOURCE_RES`) only when `text` names it. In source the * hook holds to definitions a place is a definition, whatever names the * file: not a file's preamble and, unless the query asks for tests, not its - * module-level code. */ -function isPlace(unit, asks, text) { + * module-level code. + * + * `firstPick` says the unit is the first result of a picked answer. There + * `other` is a place as a definition is: module-level code, the lines of a + * class outside its methods, a window of a file the index could not parse. + * The rule on definitions is for an answer no model has read. The pick + * model's candidates hold such units, and it read the request against them + * before it put this one first; a fix can be made outside any definition, + * in a setting or a class attribute. The rules on the path hold for that + * unit, and a file's preamble is no place there either. */ +function isPlace(unit, asks, text, firstPick) { const path = String(unit.path || ""); if (!path) return false; const named = namesFile(text, path); @@ -582,7 +592,7 @@ function isPlace(unit, asks, text) { if (!named) return false; } else if (!named && ((matches(DOCUMENT_RES) && !asks.docs) || matches(NOT_SOURCE_RES))) return false; if (DEFINITION_KINDS.has(unit.kind) || !DEFINITION_SOURCE_RE.test(path)) return true; - return asks.tests && unit.kind === "other"; + return (asks.tests || firstPick) && unit.kind === "other"; } const firstLine = (unit) => Number(unit.start_line || 0); @@ -639,11 +649,13 @@ export function places(res, { query, text = query, cap = 3, picked = false } = { const asks = { tests: asksForTests(query), docs: asksForDocs(query) }; const prompt = String(text || ""); const results = (res?.results || []).slice(0, picked ? cap : undefined); - if (picked && results.length && !isPlace(results[0], asks, prompt)) return []; + // Of a picked answer the reply proves one unit a pick, the first: it marks none below it. + const place = (unit) => isPlace(unit, asks, prompt, picked && unit === results[0]); + if (picked && results.length && !place(results[0])) return []; const seen = new Set(); const ranked = results.filter((unit) => { const pointer = pointerOf(unit); - if (seen.has(pointer) || !isPlace(unit, asks, prompt)) return false; + if (seen.has(pointer) || !place(unit)) return false; seen.add(pointer); return true; }); diff --git a/tests/unit/session_content_senders.json b/tests/unit/session_content_senders.json index 5d2230ac27..4c37eba14c 100644 --- a/tests/unit/session_content_senders.json +++ b/tests/unit/session_content_senders.json @@ -390,14 +390,14 @@ "integrations/claude-code-plugin/hooks/loom-code-shunt.mjs": "f4f761e9f5ed207c8cf4abd6759c1401f9354cddcae2b9065bda9835d778f04b", "integrations/claude-code-plugin/hooks/loom-flush.mjs": "a7634c8a3115aa98f0d7f5d098469cc3a87af4018fb2737c1a90fd27fc19c6e0", "integrations/claude-code-plugin/hooks/loom-namespace-health.mjs": "5f2bd718a88587c5f66b9823317bc77c18ce6a124f0602368b254d2c8c733ddb", - "integrations/claude-code-plugin/hooks/loom-prompt-pack.mjs": "0fd84e94d004cb24a839cd8766088897b65b16ccca4e3338f5802fb228fd4480", + "integrations/claude-code-plugin/hooks/loom-prompt-pack.mjs": "07b43e1073ae2ab6a9a6c4530ef1d197c35b232f6ac31e455555f538b5e99834", "integrations/claude-code-plugin/hooks/loom-session-start.mjs": "31ba0130495c6cbd40a3e4e673d9709deafb0cefb0b655b117d22a2437502b78", "integrations/claude-code-plugin/lib/collections-line.mjs": "4415df00d8af1cbb64f5bc25fa1ba1176433b122c1ae35f809b3981f4439c8c9", "integrations/claude-code-plugin/lib/first-session.mjs": "ea5c41ac14f173aa81c8fefc894d72a6661be66e242d0b614f502a0de98b8646", "integrations/claude-code-plugin/lib/flush-job.mjs": "e6c4ae5e42a54cba0e171b727688be621a81245023bf85a8ac093d334bc9bfd5", "integrations/claude-code-plugin/lib/loom-client.mjs": "33b83b0977c4a39d11bf0e9de44bc6bccd6c2bfef56f1639a26b54e14a6b7f4c", "integrations/claude-code-plugin/lib/namespace-health.mjs": "c8a9aab01f1e996c97643aee7b8f367bf6447f7a6c74dec01a70dc50a0caa182", - "integrations/claude-code-plugin/lib/prompt-pack.mjs": "3afe2dd5e565d3af300954f5ca6cd3c0123fb40aed5d8c501c0aeed9f8b30563", + "integrations/claude-code-plugin/lib/prompt-pack.mjs": "cb5a69d5baa078d7dbfb7873140809dc13b5145f5fe7eae1d6a621ba7ea0c19a", "integrations/claude-code-plugin/lib/repo-scope.mjs": "1fe69b8ecaf5637dc778fb1c8593336afc74d5029f5487b9419f87bc832dd07c", "integrations/claude-code-plugin/lib/session-brief.mjs": "3aee492198a400ed0fa5fee733b4a4e04463efcc4d0b22ff760e1b765a76e28b", "integrations/claude-code-plugin/lib/session-start.mjs": "60675fae31105740885c3b76a34f50e7d18e31447d792bbea5e7c03fc1b30627", @@ -405,14 +405,14 @@ "integrations/codex-plugin/hooks/loom-code-shunt.mjs": "f4f761e9f5ed207c8cf4abd6759c1401f9354cddcae2b9065bda9835d778f04b", "integrations/codex-plugin/hooks/loom-flush.mjs": "a7634c8a3115aa98f0d7f5d098469cc3a87af4018fb2737c1a90fd27fc19c6e0", "integrations/codex-plugin/hooks/loom-namespace-health.mjs": "5f2bd718a88587c5f66b9823317bc77c18ce6a124f0602368b254d2c8c733ddb", - "integrations/codex-plugin/hooks/loom-prompt-pack.mjs": "0fd84e94d004cb24a839cd8766088897b65b16ccca4e3338f5802fb228fd4480", + "integrations/codex-plugin/hooks/loom-prompt-pack.mjs": "07b43e1073ae2ab6a9a6c4530ef1d197c35b232f6ac31e455555f538b5e99834", "integrations/codex-plugin/hooks/loom-session-start.mjs": "31ba0130495c6cbd40a3e4e673d9709deafb0cefb0b655b117d22a2437502b78", "integrations/codex-plugin/lib/collections-line.mjs": "4415df00d8af1cbb64f5bc25fa1ba1176433b122c1ae35f809b3981f4439c8c9", "integrations/codex-plugin/lib/first-session.mjs": "ea5c41ac14f173aa81c8fefc894d72a6661be66e242d0b614f502a0de98b8646", "integrations/codex-plugin/lib/flush-job.mjs": "e6c4ae5e42a54cba0e171b727688be621a81245023bf85a8ac093d334bc9bfd5", "integrations/codex-plugin/lib/loom-client.mjs": "33b83b0977c4a39d11bf0e9de44bc6bccd6c2bfef56f1639a26b54e14a6b7f4c", "integrations/codex-plugin/lib/namespace-health.mjs": "c8a9aab01f1e996c97643aee7b8f367bf6447f7a6c74dec01a70dc50a0caa182", - "integrations/codex-plugin/lib/prompt-pack.mjs": "3afe2dd5e565d3af300954f5ca6cd3c0123fb40aed5d8c501c0aeed9f8b30563", + "integrations/codex-plugin/lib/prompt-pack.mjs": "cb5a69d5baa078d7dbfb7873140809dc13b5145f5fe7eae1d6a621ba7ea0c19a", "integrations/codex-plugin/lib/repo-scope.mjs": "1fe69b8ecaf5637dc778fb1c8593336afc74d5029f5487b9419f87bc832dd07c", "integrations/codex-plugin/lib/session-brief.mjs": "3aee492198a400ed0fa5fee733b4a4e04463efcc4d0b22ff760e1b765a76e28b", "integrations/codex-plugin/lib/session-start.mjs": "60675fae31105740885c3b76a34f50e7d18e31447d792bbea5e7c03fc1b30627",