From cc3cbeee991bf92c12ae9121a3ff31b7aaed4a99 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Thu, 19 Nov 2026 00:52:57 +0000 Subject: [PATCH 01/50] Automatic hooks retain bound session privacy Keep existing lineage and every contributing producer root across response observation, detached delivery and shunt aggregation. Refuse unknown automatic context and retain unacknowledged reports. Native bootstrap and direct MCP publication remain unqualified. Signed-off-by: zlareb1 --- .../.claude-plugin/plugin.json | 2 +- integrations/claude-code-plugin/README.md | 14 +- .../hooks/loom-blast-radius.mjs | 3 +- .../hooks/loom-code-shunt.mjs | 165 +++++--- .../hooks/loom-conversation.mjs | 8 +- .../hooks/loom-file-context.mjs | 3 +- .../claude-code-plugin/hooks/loom-flush.mjs | 2 +- .../claude-code-plugin/hooks/loom-run.mjs | 5 + .../hooks/loom-test-gate.mjs | 3 +- .../hooks/loom-validate-gate.mjs | 3 +- .../claude-code-plugin/lib/flush-job.mjs | 76 ++-- .../lib/hook-protection.mjs | 47 +++ .../claude-code-plugin/lib/loom-client.mjs | 55 ++- .../lib/session-response.mjs | 134 +++++++ .../claude-code-plugin/lib/shunt-ledger.mjs | 56 ++- .../claude-code-plugin/lib/tmp-state.mjs | 6 +- .../tests/agent-mode-fallback.test.mjs | 7 +- .../tests/code-hooks-failopen.test.mjs | 46 ++- .../tests/code-shunt-hook.test.mjs | 163 +++++++- .../tests/established-session.mjs | 17 + .../tests/file-rules-team.test.mjs | 4 +- .../tests/flush-hook.test.mjs | 16 +- .../tests/flush-job-protection.test.mjs | 249 ++++++++++++ .../tests/flush-job.test.mjs | 37 +- .../tests/hook-contract.test.mjs | 12 +- .../tests/hook-protection.test.mjs | 150 +++++++ .../tests/install-token-notice.test.mjs | 10 +- .../tests/private-tmp-state.test.mjs | 2 + .../tests/prompt-pack-hook.test.mjs | 2 + .../tests/recording.test.mjs | 2 + .../tests/run-id-stash.test.mjs | 12 + .../tests/server-version.test.mjs | 4 +- .../tests/session-brief.test.mjs | 3 + .../session-lineage-propagation.test.mjs | 368 ++++++++++++++++++ .../tests/session-response.test.mjs | 242 ++++++++++++ .../tests/shunt-protection.test.mjs | 176 +++++++++ .../tests/transcript.test.mjs | 5 + .../codex-plugin/.codex-plugin/plugin.json | 2 +- integrations/codex-plugin/README.md | 17 +- integrations/codex-plugin/hooks/hooks.json | 2 +- .../codex-plugin/hooks/loom-blast-radius.mjs | 3 +- .../codex-plugin/hooks/loom-code-shunt.mjs | 165 +++++--- .../codex-plugin/hooks/loom-conversation.mjs | 8 +- .../codex-plugin/hooks/loom-file-context.mjs | 3 +- .../codex-plugin/hooks/loom-flush.mjs | 2 +- integrations/codex-plugin/hooks/loom-run.mjs | 5 + .../codex-plugin/hooks/loom-test-gate.mjs | 3 +- .../codex-plugin/hooks/loom-validate-gate.mjs | 3 +- integrations/codex-plugin/lib/flush-job.mjs | 76 ++-- .../codex-plugin/lib/hook-protection.mjs | 47 +++ integrations/codex-plugin/lib/loom-client.mjs | 55 ++- .../codex-plugin/lib/session-response.mjs | 134 +++++++ .../codex-plugin/lib/shunt-ledger.mjs | 56 ++- integrations/codex-plugin/lib/tmp-state.mjs | 6 +- 54 files changed, 2421 insertions(+), 275 deletions(-) create mode 100644 integrations/claude-code-plugin/lib/hook-protection.mjs create mode 100644 integrations/claude-code-plugin/lib/session-response.mjs create mode 100644 integrations/claude-code-plugin/tests/established-session.mjs create mode 100644 integrations/claude-code-plugin/tests/flush-job-protection.test.mjs create mode 100644 integrations/claude-code-plugin/tests/hook-protection.test.mjs create mode 100644 integrations/claude-code-plugin/tests/session-lineage-propagation.test.mjs create mode 100644 integrations/claude-code-plugin/tests/session-response.test.mjs create mode 100644 integrations/claude-code-plugin/tests/shunt-protection.test.mjs create mode 100644 integrations/codex-plugin/lib/hook-protection.mjs create mode 100644 integrations/codex-plugin/lib/session-response.mjs diff --git a/integrations/claude-code-plugin/.claude-plugin/plugin.json b/integrations/claude-code-plugin/.claude-plugin/plugin.json index a62501409f..76ce21fb5f 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.38", + "version": "1.2.40", "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 135d49b4ff..6cae9f8852 100644 --- a/integrations/claude-code-plugin/README.md +++ b/integrations/claude-code-plugin/README.md @@ -29,6 +29,12 @@ tokens per `tools/list`; every other op runs through `loom_op {op, arguments}`. > **Bare mode skips hooks.** `claude --bare` (chosen by some agent harnesses for prompt-cache and cost reasons) does not run PreToolUse hooks, so the token shunt and the outcome capture in this plugin do not fire there. A harness that wants the shunt under bare mode needs the SDK backend's hook path instead. +Automatic hooks require an existing binding to an unprotected session family. An +unknown, damaged or protected family stays silent, including automatic signup +and the session primer. The hooks do not create an independent native session +family or declare protection for direct MCP responses. Native session creation +and private-content publication remain unqualified. + ## Data handling The hooks and scripts send data only to the Loom server the plugin is configured @@ -101,8 +107,12 @@ Its files in the system temp directory, which can hold prompt and reply text, are also mode 0600. A queued write whose delivery process stopped before sending it stays until you replay or delete it; `loom-status` counts these and names the directory. A write the server refuses, or that cannot reach the server, is -discarded. A batch of the per-file read and grep records above that fails to -send is discarded too. Chat turns that fail are sent again at the end of the +discarded. Per-file read and grep reports retain their original producer's +privacy roots. A report with unknown or protected producer state is not sent. +Those reports and failed delivery claims stay on disk without acknowledgement; +failed claims are not replayed automatically. Pending and claimed shunt reports +are excluded from the current plugin's cleanup. Older plugin versions can still +expire older-format files. Chat turns that fail are sent again at the end of the next reply, and a brief's events at the next brief save. At session start the plugin tries to remove other state files untouched for a day. They can remain longer, especially when no new session starts. The log of which tool calls the plugin redirected diff --git a/integrations/claude-code-plugin/hooks/loom-blast-radius.mjs b/integrations/claude-code-plugin/hooks/loom-blast-radius.mjs index d152bc317e..46dfc9f556 100644 --- a/integrations/claude-code-plugin/hooks/loom-blast-radius.mjs +++ b/integrations/claude-code-plugin/hooks/loom-blast-radius.mjs @@ -24,6 +24,7 @@ import { gitToplevel, postBounded, readStdinJson, + runHookMain, DIFF_ARGS, diffHeaders, resolveServerUrl, @@ -122,4 +123,4 @@ async function main() { scheduleExit(0); } -main().catch(() => scheduleExit(0)); +runHookMain(main); diff --git a/integrations/claude-code-plugin/hooks/loom-code-shunt.mjs b/integrations/claude-code-plugin/hooks/loom-code-shunt.mjs index 4d46b098ab..cb96e57472 100755 --- a/integrations/claude-code-plugin/hooks/loom-code-shunt.mjs +++ b/integrations/claude-code-plugin/hooks/loom-code-shunt.mjs @@ -21,7 +21,7 @@ // user's decision log (tmpStatePath "shunt-log") and its unflushed copy, one // batch per reporting scope and session (repo.pending, named by batchKind in // lib/shunt-ledger.mjs), all through lib/tmp-state.mjs's private-file helpers, -// flushed as code.outcome rows (fire-and-forget, 300 ms) on a deny or every +// flushed as code.outcome rows (300 ms per request) on a deny or every // SHUNT_FLUSH_EVERY lines (lib/shunt-ledger.mjs). import fs from "node:fs"; import path from "node:path"; @@ -31,8 +31,8 @@ import { gitToplevel, pathUnder, postBounded, - postFireAndForget, readStdinJson, + runHookMain, resolveServerUrl, resolveToken, redactSecrets, @@ -69,6 +69,8 @@ import { estimateTokensRead, estimateTokensSaved, formatShuntLine, + formatShuntBatchLine, + parseShuntBatch, shouldFlushShunt, } from "../lib/shunt-ledger.mjs"; import { repoLookupOutcome, resolveIndexedRepo, resolveTimeoutMs } from "../lib/repo-scope.mjs"; @@ -82,6 +84,13 @@ import { writeSessionState, } from "../lib/tmp-state.mjs"; +import { sessionIsProtected } from "../lib/session-protection.mjs"; +import { + automaticSessionIsProtected, + sessionProtectionContext, + withCapturedSessionProtection, +} from "../lib/session-response.mjs"; + const ENABLED = envSwitch("LOOM_CODE_SHUNT", true); const MAX_LINES = Number(process.env.LOOM_SHUNT_MAX_LINES || DEFAULT_MAX_LINES); const READ_CAP = Math.max(0, Number(process.env.LOOM_SHUNT_READ_CAP || DEFAULT_READ_CAP) || 0); @@ -101,8 +110,7 @@ const LOG_PATH = tmpStatePath("shunt-log", UID, { ext: ".log" }); /** The unflushed batch, set on `repo` in main: `kind` names the reporting scope * (server, token, repository) every batch of it shares, `path` this session's - * batch, a name nobody can create ahead of the session. Batches and claimed - * batches are swept with the other state files. A flush also claims the + * batch. Unacknowledged batches and claims do not expire. A flush also claims the * scope's other batches: those ended or crashed sessions left, and those of * sessions still running. */ function pendingFor(repo, sessionId) { @@ -112,7 +120,7 @@ function pendingFor(repo, sessionId) { org: repo.org || "", namespace: repo.namespace || "", }); - return { kind, path: sessionStatePath(kind, sessionId, { ext: ".log" }) }; + return { kind, path: sessionStatePath(kind, sessionId, { ext: ".log.control" }) }; } function countLines(abs) { @@ -183,34 +191,59 @@ function fileBytes(abs) { } } -/** Append one decision locally; returns the number of unflushed lines. Sync - * appends of one short line — microseconds, and never on the network. */ -function logDecision(pending, { decision, tool, tokensSaved, reason, passReason, path, startLine, endLine }) { - if (!LOG_ON) return 0; - const line = formatShuntLine({ - decision, - tool, - tokensSaved, - reason, - passReason, - path, - startLine, - endLine, - }); +/** Only a newly created lock may be released. A crashed producer leaves evidence. */ +function withBatchLock(file, callback) { + const lock = `${file}.lock`; try { - writePrivateFile(LOG_PATH, line, { append: true }); + fs.mkdirSync(lock, { mode: 0o700 }); } catch { - /* tmp not writable, or a file we do not own: fine */ + return undefined; } try { - writePrivateFile(pending.path, line, { append: true }); - const text = readPrivateFile(pending.path); - return text.split("\n").length - 1; + return callback(); + } finally { + try { + fs.rmdirSync(lock); + } catch { + /* Leave a lock we cannot release; never force another writer's state. */ + } + } +} + +function rootsAreClean(roots) { + try { + return Boolean(roots?.length) && roots.every((root) => !sessionIsProtected(root)); } catch { - return 0; + return false; } } +/** The diagnostic TSV stays readable; only the separate queue carries control. */ +function logDecision(pending, { decision, tool, tokensSaved, reason, passReason, path, startLine, endLine }) { + if (!LOG_ON || automaticSessionIsProtected()) return 0; + const roots = sessionProtectionContext()?.roots; + const line = formatShuntLine({ decision, tool, tokensSaved, reason, passReason, path, startLine, endLine }); + const row = formatShuntBatchLine(line, roots); + if (!row || !rootsAreClean(roots)) return 0; + try { + if (automaticSessionIsProtected()) return 0; + writePrivateFile(LOG_PATH, line, { append: true }); + } catch { + /* tmp not writable, or a file we do not own: fine */ + } + return withBatchLock(pending.path, () => { + try { + // Do not append over damaged or unattributed evidence. + if (fs.existsSync(pending.path) && !parseShuntBatch(readPrivateFile(pending.path))) return 0; + if (automaticSessionIsProtected()) return 0; + writePrivateFile(pending.path, row, { append: true }); + return parseShuntBatch(readPrivateFile(pending.path))?.lines || 0; + } catch { + return 0; + } + }) || 0; +} + /** The batch files of this reporting scope that this user owns: this * session's and any other session's. Another user's file, a link or a hard * link is left alone. */ @@ -224,7 +257,7 @@ function scopeBatches(pending) { return []; } return names - .filter((n) => n.startsWith(prefix) && n.endsWith(".log")) + .filter((n) => n.startsWith(prefix) && n.endsWith(".log.control")) .map((n) => path.join(dir, n)) .filter((file) => { try { @@ -236,40 +269,56 @@ function scopeBatches(pending) { }); } -/** Claim one batch by renaming it to a name of this process, read it with the - * private-file checks, and remove it: of two hooks flushing at once only one - * gets each batch. "" when another hook claimed it first, or it is not ours. */ +/** Locking serializes appends and rename, so a claimed file has no producer writer. */ function claimBatch(file) { - const claimed = tmpStatePath("shunt-claim", `${file}:${process.pid}:${crypto.randomBytes(8).toString("hex")}`, { - ext: ".log", + return withBatchLock(file, () => { + let claimed; + try { + const before = parseShuntBatch(readPrivateFile(file)); + if (!before || !rootsAreClean(before.roots)) return null; + claimed = tmpStatePath("shunt-protected-claim", `${file}:${process.pid}:${crypto.randomBytes(8).toString("hex")}`, { + ext: ".log.control", + }); + fs.renameSync(file, claimed); + // Re-read after claim: a changed control record is never sent as the old one. + const batch = parseShuntBatch(readPrivateFile(claimed)); + return { claimed, batch }; + } catch { + return claimed ? { claimed, batch: null } : null; + } }); +} + +/** Acknowledge only after every bounded post and the final producer-root check. */ +async function flushPending(repo) { + if (!REPORT_ON || automaticSessionIsProtected()) return; + const claims = scopeBatches(repo.pending).map(claimBatch).filter(Boolean); + if (!claims.length) return; + const ready = claims.filter(({ batch }) => batch && rootsAreClean(batch.roots)); + const roots = ready.flatMap(({ batch }) => batch.roots); + let acknowledged = false; try { - fs.renameSync(file, claimed); - } catch { - return ""; - } - let text = ""; - try { - text = readPrivateFile(claimed); + await withCapturedSessionProtection(roots, async () => { + const agg = aggregateShuntLog(ready.map(({ batch }) => batch.text).join("")); + const bodies = [agg.hit, ...agg.passes].filter(Boolean); + if (!bodies.length) return; + const scope = { org: repo.org || "", namespace: repo.namespace || "", consumer: "claude-code-shunt" }; + for (const body of bodies) { + if (automaticSessionIsProtected()) return; + const answer = await postBounded("code.outcome", { ...scope, ...body }, { timeoutMs: FLUSH_TIMEOUT_MS }); + if (!answer || automaticSessionIsProtected()) return; + } + acknowledged = !automaticSessionIsProtected(); + }); } catch { - /* replaced between the check and the rename: not ours to post */ + // Observer or control failure withholds acknowledgment; claims retain their evidence. + } finally { + for (const claim of claims) { + if (acknowledged && ready.includes(claim) && rootsAreClean(roots)) removeFile(claim.claimed); + // Refusals and ambiguous outcomes stay outside the automatic queue. + // Keeping the claim avoids replaying an unchanged protected operation. + } } - removeFile(claimed); - return text; -} - -/** Ship the scope's pending batches as code.outcome rows. Fire-and-forget with - * a 300 ms ceiling; a lost batch costs a few counter ticks. Each batch is - * claimed before it is read, so it is posted at most once. */ -function flushPending(repo) { - if (!REPORT_ON) return; - const text = scopeBatches(repo.pending).map(claimBatch).join(""); - if (!text) return; - const agg = aggregateShuntLog(text); - const opts = { timeoutMs: FLUSH_TIMEOUT_MS }; - const scope = { org: repo.org || "", namespace: repo.namespace || "", consumer: "claude-code-shunt" }; - if (agg.hit) postFireAndForget("code.outcome", { ...scope, ...agg.hit }, opts); - for (const body of agg.passes) postFireAndForget("code.outcome", { ...scope, ...body }, opts); } /** Every decision funnels through here: deny + log + maybe flush. @@ -288,7 +337,7 @@ function settle(repo, { tool, verdict, tokens, path = "", span = { start: 0, end startLine: span.start, endLine: span.end, }); - if (shouldFlushShunt({ denied, pending: unflushed })) flushPending(repo); + if (shouldFlushShunt({ denied, pending: unflushed })) void flushPending(repo); return denied; } @@ -562,6 +611,4 @@ async function settleBashBulk(repo, { bulk, cwd, sessionId, ctxBase }) { return settle(repo, { tool: "Bash:grep", verdict, tokens: 0, path: rel || "" }); } -main() - .catch(() => {}) - .finally(() => scheduleExit(0)); +runHookMain(main).finally(() => scheduleExit(0)); diff --git a/integrations/claude-code-plugin/hooks/loom-conversation.mjs b/integrations/claude-code-plugin/hooks/loom-conversation.mjs index 30095a1813..16f2308651 100644 --- a/integrations/claude-code-plugin/hooks/loom-conversation.mjs +++ b/integrations/claude-code-plugin/hooks/loom-conversation.mjs @@ -30,6 +30,7 @@ import { } from "../lib/loom-client.mjs"; import { dispatchFlush } from "../lib/flush-job.mjs"; import { readRecording } from "../lib/recording.mjs"; +import { automaticSessionIsProtected } from "../lib/session-response.mjs"; import { readCursor, writeCursor } from "../lib/tmp-state.mjs"; import { fileSize, readLinesFrom } from "../lib/transcript.mjs"; import { appendTurn, journalCursorPath, readJournal, readJournalCursor, rowsSince } from "../lib/turn-journal.mjs"; @@ -82,6 +83,7 @@ function turnFrom(obj) { * only when all land. Falls back to inline fire-and-forget without * advancing the cursor, so the next Stop retries. */ function dispatch(appends, cursorFile, advanceTo) { + if (automaticSessionIsProtected()) return; // The owner switched recording off for this namespace (the // server said so on a recent append): nothing would be kept, so send // nothing and move the cursor past these turns. Re-asked after the cache's @@ -91,7 +93,7 @@ function dispatch(appends, cursorFile, advanceTo) { writeCursor(cursorFile, advanceTo); return; } - if (!dispatchFlush({ appends, cursorPath: cursorFile, advanceTo })) { + if (!dispatchFlush({ appends, cursorPath: cursorFile, advanceTo }) && !automaticSessionIsProtected()) { for (const a of appends) postFireAndForget("conversation.append", a); } } @@ -103,6 +105,7 @@ function dispatch(appends, cursorFile, advanceTo) { * on, so a Stop carries the prompt(s) since the last Stop plus the reply. * Returns true when it handled the event. */ function fromEvents(payload, sessionId, scope) { + if (automaticSessionIsProtected()) return true; const reply = typeof payload.last_assistant_message === "string" ? payload.last_assistant_message.trim() : ""; if (!reply) return false; appendTurn(sessionId, "assistant", reply); @@ -130,6 +133,7 @@ async function main() { return scheduleExit(0); } const payload = await readStdinJson(); + if (automaticSessionIsProtected()) return scheduleExit(0); const sessionId = String(payload.session_id || ""); const transcript = String(payload.transcript_path || ""); if (!sessionId) return scheduleExit(0); @@ -141,6 +145,7 @@ async function main() { // Let a just-finished assistant message flush before we read (see SETTLE_MS). if (SETTLE_MS > 0) await new Promise((r) => setTimeout(r, SETTLE_MS)); + if (automaticSessionIsProtected()) return scheduleExit(0); if (!fileSize(transcript)) { fromEvents(payload, sessionId, scope); // transcript named but unreadable @@ -179,6 +184,7 @@ async function main() { }); } + if (automaticSessionIsProtected()) return scheduleExit(0); if (appends.length === 0) { // Nothing new — advance past scanned lines and exit. A transcript in a // shape this script does not parse yields nothing every time; the diff --git a/integrations/claude-code-plugin/hooks/loom-file-context.mjs b/integrations/claude-code-plugin/hooks/loom-file-context.mjs index 161161af3c..d7ce7eea38 100644 --- a/integrations/claude-code-plugin/hooks/loom-file-context.mjs +++ b/integrations/claude-code-plugin/hooks/loom-file-context.mjs @@ -25,6 +25,7 @@ import { gitToplevel, postBounded, readStdinJson, + runHookMain, resolveScope, resolveServerUrl, resolveToken, @@ -98,4 +99,4 @@ async function main() { scheduleExit(0); } -main().catch(() => scheduleExit(0)); +runHookMain(main); diff --git a/integrations/claude-code-plugin/hooks/loom-flush.mjs b/integrations/claude-code-plugin/hooks/loom-flush.mjs index 5b059d88f2..4bd214fae7 100644 --- a/integrations/claude-code-plugin/hooks/loom-flush.mjs +++ b/integrations/claude-code-plugin/hooks/loom-flush.mjs @@ -62,4 +62,4 @@ async function main() { removeFile(jobPath); } -runHookMain(main); +runHookMain(main, { detachedJob: true }); diff --git a/integrations/claude-code-plugin/hooks/loom-run.mjs b/integrations/claude-code-plugin/hooks/loom-run.mjs index 0a7dca9741..4bf48f5ced 100644 --- a/integrations/claude-code-plugin/hooks/loom-run.mjs +++ b/integrations/claude-code-plugin/hooks/loom-run.mjs @@ -41,6 +41,7 @@ import { } from "../lib/run-hook.mjs"; import { sessionStatePath, writeJsonFile } from "../lib/tmp-state.mjs"; import { appendTurn, lastAssistant, readJournal } from "../lib/turn-journal.mjs"; +import { automaticSessionIsProtected } from "../lib/session-response.mjs"; const ENABLED = envSwitch("LOOM_RUN_LEDGER", true); @@ -69,6 +70,7 @@ async function main() { const prompt = String(payload.prompt || ""); // Every prompt goes to the turn journal: the consumers // read it when the client hands them no parseable transcript. + if (automaticSessionIsProtected()) return scheduleExit(0); appendTurn(sessionId, "user", prompt); const prior = readRunState(sessionId); if (prior.begun || prior.refused) { @@ -80,6 +82,7 @@ async function main() { // the first redacted body (the README's hooks table has the retry rules). const body = prior.attempted && prior.body ? prior.body : beginBody(scope, payload, prompt); const res = await postBoundedResult("run.begin", body, opts); + if (automaticSessionIsProtected()) return scheduleExit(0); const runId = res.status >= 200 && res.status < 300 && res.body ? res.body.run_id : ""; // 408, 429 and a 2xx with no usable id may still have opened the run. const transient = res.status === 408 || res.status === 429 || res.status >= 500 || res.status < 300; @@ -101,6 +104,7 @@ async function main() { const deadline = Date.now() + 900; const beginOpts = { ...opts, timeoutMs: Math.min(opts.timeoutMs, 400) }; const res = await postBoundedResult("run.begin", state.body || beginBody(scope, payload, ""), beginOpts); + if (automaticSessionIsProtected()) return scheduleExit(0); const runId = res.status >= 200 && res.status < 300 && res.body ? res.body.run_id : ""; if (typeof runId === "string" && runId) writeRunBegun(sessionId, runId); state = readRunState(sessionId); @@ -122,6 +126,7 @@ async function main() { // No longer begun, so a resumed session's next prompt opens a new run. The // id stays for the other SessionEnd hooks, which run beside this one; the // stale-file sweep removes it later. + if (automaticSessionIsProtected()) return scheduleExit(0); writeJsonFile(sp, { run_id: state.run_id }); await postBounded("run.complete", body, completeOpts); // The stash is removed by loom-session-memory, the last SessionEnd hook. diff --git a/integrations/claude-code-plugin/hooks/loom-test-gate.mjs b/integrations/claude-code-plugin/hooks/loom-test-gate.mjs index b4084c7bfa..e3b27da68c 100644 --- a/integrations/claude-code-plugin/hooks/loom-test-gate.mjs +++ b/integrations/claude-code-plugin/hooks/loom-test-gate.mjs @@ -24,6 +24,7 @@ import { pathUnder, postBounded, readStdinJson, + runHookMain, DIFF_ARGS, diffHeaders, resolveServerUrl, @@ -140,4 +141,4 @@ async function main() { scheduleExit(0); } -main().catch(() => scheduleExit(0)); +runHookMain(main); diff --git a/integrations/claude-code-plugin/hooks/loom-validate-gate.mjs b/integrations/claude-code-plugin/hooks/loom-validate-gate.mjs index 13be736cde..2b5413f7a4 100644 --- a/integrations/claude-code-plugin/hooks/loom-validate-gate.mjs +++ b/integrations/claude-code-plugin/hooks/loom-validate-gate.mjs @@ -14,6 +14,7 @@ import { postBounded, readStdinJson, + runHookMain, resolveScope, resolveServerUrl, resolveToken, @@ -71,4 +72,4 @@ async function main() { scheduleExit(0); } -main().catch(() => scheduleExit(0)); +runHookMain(main); diff --git a/integrations/claude-code-plugin/lib/flush-job.mjs b/integrations/claude-code-plugin/lib/flush-job.mjs index 98bf476cb1..26d7c4693a 100644 --- a/integrations/claude-code-plugin/lib/flush-job.mjs +++ b/integrations/claude-code-plugin/lib/flush-job.mjs @@ -20,6 +20,8 @@ import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; +import { sessionIsProtected } from "./session-protection.mjs"; +import { sessionProtectionContext, withCapturedSessionProtection } from "./session-response.mjs"; import { writePrivateFile } from "./tmp-state.mjs"; const FLUSHER = fileURLToPath(new URL("../hooks/loom-flush.mjs", import.meta.url)); @@ -33,14 +35,20 @@ export function flushJobPath(tmp = os.tmpdir(), now = Date.now(), pid = process. * True when the flusher was started; false (job file removed) otherwise. */ export function dispatchFlush(job, { timeoutMs = 15000, spawnImpl = spawn, tmp } = {}) { + const roots = sessionProtectionContext()?.roots; + if (!roots?.length || roots.some(sessionIsProtected)) return false; const jobPath = flushJobPath(tmp); try { // The job carries session text: owner-only, like the other tmp state. - writePrivateFile(jobPath, JSON.stringify(job)); + writePrivateFile(jobPath, JSON.stringify({ ...job, protectionRoots: roots.map(({ base, id }) => ({ base, id })) })); } catch { return false; } try { + if (roots.some(sessionIsProtected)) { + fs.unlinkSync(jobPath); + return false; + } const child = spawnImpl(process.execPath, [FLUSHER, jobPath], { detached: true, stdio: "ignore", @@ -73,30 +81,48 @@ export function dispatchFlush(job, { timeoutMs = 15000, spawnImpl = spawn, tmp } */ export async function runFlushJob(input, { post, cursor, noteRecording = () => {} }) { const job = input && typeof input === "object" ? input : {}; - const appends = Array.isArray(job.appends) ? job.appends : []; - const posts = (Array.isArray(job.posts) ? job.posts : []).filter((p) => p && typeof p.op === "string" && p.body); - const settled = async (calls) => - (await Promise.allSettled(calls)).map((r) => (r.status === "fulfilled" ? r.value : null)); - - let postsOk = false; - if (posts.length) { - const replies = await settled(posts.map((p) => post(p.op, p.body))); - postsOk = replies.every(Boolean); - } - // A sender that moved its cursor ahead optimistically names rollbackTo: on - // failure the cursor goes back, unless a later job has moved it on since. - if (!appends.length && job.cursorPath && Number.isInteger(job.rollbackTo) && !postsOk) { - if (cursor.read(job.cursorPath).trim() === String(job.advanceTo)) cursor.write(job.cursorPath, job.rollbackTo); + const roots = Object.freeze( + (Array.isArray(job.protectionRoots) ? job.protectionRoots : []).map((root) => + Object.freeze({ base: root?.base, id: root?.id }), + ), + ); + const protectedNow = () => !roots.length || roots.some(sessionIsProtected); + const rollback = () => { + if (!(Array.isArray(job.appends) && job.appends.length) && job.cursorPath && Number.isInteger(job.rollbackTo)) { + if (cursor.read(job.cursorPath).trim() === String(job.advanceTo)) cursor.write(job.cursorPath, job.rollbackTo); + } + }; + if (protectedNow()) { + rollback(); + return { postsOk: false, appendsOk: false }; } + const result = withCapturedSessionProtection(roots, async () => { + const send = (op, body) => (protectedNow() ? null : post(op, body)); + const appends = Array.isArray(job.appends) ? job.appends : []; + const posts = (Array.isArray(job.posts) ? job.posts : []).filter((p) => p && typeof p.op === "string" && p.body); + const settled = async (calls) => + (await Promise.allSettled(calls)).map((r) => (r.status === "fulfilled" ? r.value : null)); - let appendsOk = false; - if (appends.length) { - const replies = await settled(appends.map((a) => post("conversation.append", a))); - appendsOk = replies.every(Boolean); - noteRecording(appends, replies); - // The cursor moves only when every turn landed; otherwise the next Stop - // re-reads and retries (msg_id makes a resend idempotent). - if (appendsOk && job.cursorPath && Number.isInteger(job.advanceTo)) cursor.write(job.cursorPath, job.advanceTo); - } - return { postsOk, appendsOk }; + let postsOk = false; + if (posts.length) { + const replies = await settled(posts.map((p) => send(p.op, p.body))); + postsOk = replies.every(Boolean) && !protectedNow(); + } + // A failed or suppressed post must not acknowledge an optimistic cursor. + if (!postsOk) rollback(); + + let appendsOk = false; + if (appends.length) { + const replies = await settled(appends.map((a) => send("conversation.append", a))); + appendsOk = replies.every(Boolean) && !protectedNow(); + if (!protectedNow()) noteRecording(appends, replies); + if (appendsOk && !protectedNow() && job.cursorPath && Number.isInteger(job.advanceTo)) { + cursor.write(job.cursorPath, job.advanceTo); + } + } + return { postsOk, appendsOk }; + }); + if (result !== undefined) return result; + rollback(); + return { postsOk: false, appendsOk: false }; } diff --git a/integrations/claude-code-plugin/lib/hook-protection.mjs b/integrations/claude-code-plugin/lib/hook-protection.mjs new file mode 100644 index 0000000000..e3835507a9 --- /dev/null +++ b/integrations/claude-code-plugin/lib/hook-protection.mjs @@ -0,0 +1,47 @@ +// A passive hook resolves existing lineage; it never establishes a parent edge. +import { + findProtectionAlias, + protectSession, + SessionProtectionUnavailable, +} from "./session-protection.mjs"; + +function sameRoot(first, second) { + return first?.base === second?.base && first?.id === second?.id; +} + +function refuse(roots) { + for (const root of roots) { + try { + protectSession(root); + } catch { + // A failed mark must not leave another known contributor clean. + } + } + throw new SessionProtectionUnavailable(); +} + +/** Only canonical identity and existing application-owned anchors are authority. */ +export function bindHookProtection(payload, { host, event, base, knownRoots = [] } = {}) { + const roots = [...knownRoots]; + try { + if ( + !["claude-code", "codex"].includes(host) || + !payload || + typeof payload !== "object" || + Array.isArray(payload) || + typeof payload.session_id !== "string" || + !payload.session_id || + payload.session_id.length > 1024 + ) throw new SessionProtectionUnavailable(); + // Resolve this anchor even when another known contributor is unusable. + const root = findProtectionAlias(host, payload.session_id, { base }); + if (root) roots.push(root); + if (typeof event !== "string" || !event || (payload.hook_event_name !== undefined && payload.hook_event_name !== event)) { + throw new SessionProtectionUnavailable(); + } + if (!root || roots.some((anchor) => !sameRoot(anchor, root))) throw new SessionProtectionUnavailable(); + return root; + } catch { + return refuse(roots); + } +} diff --git a/integrations/claude-code-plugin/lib/loom-client.mjs b/integrations/claude-code-plugin/lib/loom-client.mjs index 01b9c6668a..4daa5cf824 100644 --- a/integrations/claude-code-plugin/lib/loom-client.mjs +++ b/integrations/claude-code-plugin/lib/loom-client.mjs @@ -31,6 +31,16 @@ import { fileURLToPath } from "node:url"; // than that, and a shorter grace would kill a fire-and-forget post before it // left a slow link. import { DEFAULT_BOUND_MS, boundMs, rttCachePath } from "./hook-bounds.mjs"; +import { bindHookProtection } from "./hook-protection.mjs"; +import { SessionProtectionUnavailable } from "./session-protection.mjs"; +import { + automaticSessionIsProtected, + observeSessionResponse, + observeSessionResponses, + refuseSessionResponse, + sessionProtectionContext, + withAutomaticSessionProtection, +} from "./session-response.mjs"; import { readJsonFile, tmpStatePath, writeJsonFile } from "./tmp-state.mjs"; let _rttCache; @@ -731,6 +741,10 @@ export async function requestJson( { method = "POST", body, token = "", timeoutMs = requestTimeoutMs(), headers = {}, fetchImpl = fetch } = {}, ) { try { + const protection = sessionProtectionContext(); + if (protection?.automatic && automaticSessionIsProtected()) { + return { status: 0, body: null, suppressed: true }; + } const res = await fetchImpl(url, { method, headers: { @@ -741,11 +755,17 @@ export async function requestJson( ...(body === undefined ? {} : { body: JSON.stringify(body) }), signal: AbortSignal.timeout(timeoutMs), }); + if (res.status === 204 || res.status === 304) return { status: res.status, body: null }; let parsed = null; try { parsed = await res.json(); } catch { - /* an error body may not be JSON */ + if (protection) refuseSessionResponse(protection.roots); + /* an unbound error body may not be JSON */ + } + if (protection) { + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) refuseSessionResponse(protection.roots); + observeSessionResponses(protection.roots, parsed); } return { status: res.status, body: parsed }; } catch (e) { @@ -1009,11 +1029,23 @@ export function diffHeaders(diff, max) { * fails the host session. With LOOM_HOOK_DEBUG=on the error's first line, * redacted, goes to stderr, which is how a broken hook shows itself. */ -export function runHookMain(main) { - Promise.resolve() - .then(main) +export function runHookMain(main, { detachedJob = false } = {}) { + return Promise.resolve() + .then(async () => { + // A detached flusher restores only the producer roots in its private job. + if (detachedJob) return main(); + const raw = await rawHookPayload(); + const root = bindHookProtection(raw, { + host: process.argv[3] || "claude-code", + event: process.argv[2], + knownRoots: sessionProtectionContext()?.roots, + }); + const reply = normalizePayload(raw).tool_response; + observeSessionResponse(root, typeof reply === "string" ? { content: [{ type: "text", text: reply }] } : reply); + return withAutomaticSessionProtection(root, main); + }) .catch((e) => { - if (envSwitch("LOOM_HOOK_DEBUG", false)) { + if (!(e instanceof SessionProtectionUnavailable) && envSwitch("LOOM_HOOK_DEBUG", false)) { const line = String((e && (e.stack || e.message)) || e).split("\n")[0]; process.stderr.write(`loom hook ${basename(process.argv[1] || "")}: ${redactHead(line, 300).slice(0, 300)}\n`); } @@ -1079,16 +1111,18 @@ export function normalizePayload(raw) { * gives up waiting after a second, so a host that never closes stdin * cannot hold a hook open. */ const MAX_STDIN_CHARS = 32 * 1024 * 1024; +let hookPayload; -export function readStdinJson() { - return new Promise((resolve) => { +function rawHookPayload() { + hookPayload ??= new Promise((resolve) => { let raw = ""; let settled = false; const done = () => { if (settled) return; settled = true; try { - resolve(normalizePayload(raw.trim() ? JSON.parse(raw) : {})); + const payload = raw.trim() ? JSON.parse(raw) : {}; + resolve(payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {}); } catch { resolve({}); } @@ -1110,4 +1144,9 @@ export function readStdinJson() { } setTimeout(done, 1000).unref(); }); + return hookPayload; +} + +export function readStdinJson() { + return rawHookPayload().then(normalizePayload); } diff --git a/integrations/claude-code-plugin/lib/session-response.mjs b/integrations/claude-code-plugin/lib/session-response.mjs new file mode 100644 index 0000000000..87442f3664 --- /dev/null +++ b/integrations/claude-code-plugin/lib/session-response.mjs @@ -0,0 +1,134 @@ +// Response observation precedes the caller's rendering and automatic callbacks. +// A binding is application-owned; it is never selected by wire arguments. + +import { AsyncLocalStorage } from "node:async_hooks"; + +import { protectSession, sessionIsProtected, SessionProtectionUnavailable } from "./session-protection.mjs"; + +const context = new AsyncLocalStorage(); +const MAX_ENVELOPES = 10000; +const MAX_TEXT_BYTES = 4 * 1024 * 1024; + +export function sessionProtectionContext() { + return context.getStore(); +} + +/** Automatic work never treats missing lineage as a clean family. */ +export function automaticSessionIsProtected() { + const roots = context.getStore()?.roots; + return !roots?.length || roots.some(sessionIsProtected); +} + +function retainedRoots(roots) { + const known = new Map(); + for (const root of roots) { + const copy = Object.freeze({ base: root?.base, id: root?.id }); + known.set(JSON.stringify([copy.base, copy.id]), copy); + } + return Object.freeze([...known.values()]); +} + +function joinedRoots(root) { + return retainedRoots([...(context.getStore()?.roots ?? []), root]); +} + +/** Explicit operations remain available while retaining every response observer. */ +export function withSessionProtection(root, callback) { + return context.run(Object.freeze({ roots: joinedRoots(root), automatic: false }), callback); +} + +/** A detached producer resumes its own frozen control, independent of its flusher. */ +export function withCapturedSessionProtection(roots, callback) { + const retained = retainedRoots(roots); + if (!retained.length || retained.some(sessionIsProtected)) return undefined; + return context.run(Object.freeze({ roots: retained, automatic: true }), callback); +} + +/** Nested automatic callbacks cannot drop any contributing parent's protection. */ +export function withAutomaticSessionProtection(root, callback) { + return withCapturedSessionProtection(joinedRoots(root), callback); +} + +function markedEnvelope(value) { + const pending = [value]; + const visited = new Set(); + let remaining = MAX_ENVELOPES; + let textBytes = MAX_TEXT_BYTES; + while (pending.length) { + if (--remaining < 0) throw new SessionProtectionUnavailable(); + const item = pending.pop(); + if (!item || typeof item !== "object" || Array.isArray(item)) continue; + if (visited.has(item)) throw new SessionProtectionUnavailable(); + visited.add(item); + if (Object.hasOwn(item, "mirrored") && typeof item.mirrored !== "boolean") { + throw new SessionProtectionUnavailable(); + } + if (item.mirrored === true) return true; + for (const key of ["result", "structuredContent"]) { + if (item[key] && typeof item[key] === "object") pending.push(item[key]); + } + // JSON-RPC failures retain response metadata in error.data after unwrapping. + if (item.error && typeof item.error === "object" && item.error.data && typeof item.error.data === "object") { + pending.push(item.error.data); + } + if (Array.isArray(item.items)) { + if (pending.length + item.items.length > remaining) throw new SessionProtectionUnavailable(); + pending.push(...item.items); + } + if (Array.isArray(item.content)) { + if (pending.length + item.content.length > remaining) throw new SessionProtectionUnavailable(); + remaining -= item.content.length; + for (const content of item.content) { + if (content?.type !== "text" || typeof content.text !== "string") continue; + // Charge every block before parsing; repeated source text must not + // allocate a separate unbounded decoded object for each occurrence. + if (content.text.length > textBytes) throw new SessionProtectionUnavailable(); + textBytes -= Buffer.byteLength(content.text, "utf8"); + if (textBytes < 0) throw new SessionProtectionUnavailable(); + const text = content.text.trim(); + if (!text.startsWith("{")) continue; + try { + pending.push(JSON.parse(text)); + } catch { + throw new SessionProtectionUnavailable(); + } + } + } + } + return false; +} + +function markEveryFamily(roots) { + let failed = false; + for (const root of roots) { + try { + protectSession(root); + } catch { + failed = true; + } + } + return failed; +} + +/** Unknown response state protects every known family before withholding output. */ +export function refuseSessionResponse(roots) { + markEveryFamily(roots); + throw new SessionProtectionUnavailable(); +} + +/** Parse once and attempt every durable mark, including after another mark fails. */ +export function observeSessionResponses(roots, value) { + let marked; + try { + marked = markedEnvelope(value); + } catch { + return refuseSessionResponse(roots); + } + if (marked && markEveryFamily(roots)) throw new SessionProtectionUnavailable(); + return value; +} + +/** Consume only response envelopes; arbitrary arguments cannot choose a binding. */ +export function observeSessionResponse(root, value) { + return observeSessionResponses(joinedRoots(root), value); +} diff --git a/integrations/claude-code-plugin/lib/shunt-ledger.mjs b/integrations/claude-code-plugin/lib/shunt-ledger.mjs index 1959975977..14a24a0da1 100644 --- a/integrations/claude-code-plugin/lib/shunt-ledger.mjs +++ b/integrations/claude-code-plugin/lib/shunt-ledger.mjs @@ -6,6 +6,7 @@ // per pass reason), so tokens kept out and let in are a production series. import { createHash } from "node:crypto"; +import path from "node:path"; import { PASS_REASONS, readSpan } from "./code-shunt.mjs"; @@ -19,7 +20,60 @@ export const SHUNT_CLIENT_SOURCE = "claude-code-shunt"; export function batchKind({ serverUrl = "", token = "", org = "", namespace = "" } = {}) { const tokenId = createHash("sha256").update(String(token)).digest("hex").slice(0, 16); const scope = createHash("sha256").update(`${serverUrl}|${tokenId}|${org}/${namespace}`).digest("hex").slice(0, 12); - return `shunt-pending-${scope}`; + // Older consumers select shunt-pending; they must not consume root-aware rows. + return `shunt-protected-${scope}`; +} + +const ROOT_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; +const MAX_BATCH_BYTES = 4 * 1024 * 1024; + +function batchRoots(roots) { + if (!Array.isArray(roots) || !roots.length) return null; + const known = new Map(); + for (const root of roots) { + if (!root || typeof root !== "object" || Array.isArray(root) || + Object.keys(root).sort().join(",") !== "base,id" || + typeof root.base !== "string" || !path.isAbsolute(root.base) || typeof root.id !== "string" || !ROOT_ID.test(root.id)) return null; + const copy = Object.freeze({ base: root.base, id: root.id }); + known.set(JSON.stringify([copy.base, copy.id]), copy); + } + return Object.freeze([...known.values()]); +} + +function validDecisionLine(line) { + if (typeof line !== "string" || !line.endsWith("\n") || line.slice(0, -1).includes("\n")) return false; + const cells = line.slice(0, -1).split("\t"); + return cells.length === 9 && ["hit", "pass"].includes(cells[1]); +} + +/** Only the producer's captured context supplies roots; log fields carry no authority. */ +export function formatShuntBatchLine(line, roots) { + const retained = batchRoots(roots); + if (!retained || !validDecisionLine(line)) return null; + return JSON.stringify({ version: 1, protectionRoots: retained, line }) + "\n"; +} + +/** Any unattributed or damaged row withholds the entire batch, preserving its bytes. */ +export function parseShuntBatch(text) { + if (typeof text !== "string" || !text || !text.endsWith("\n") || Buffer.byteLength(text, "utf8") > MAX_BATCH_BYTES) return null; + const roots = []; + const lines = []; + for (const raw of text.slice(0, -1).split("\n")) { + let row; + try { + row = JSON.parse(raw); + } catch { + return null; + } + if (!row || typeof row !== "object" || Array.isArray(row) || + Object.keys(row).sort().join(",") !== "line,protectionRoots,version" || row.version !== 1 || + !validDecisionLine(row.line)) return null; + const retained = batchRoots(row.protectionRoots); + if (!retained) return null; + roots.push(...retained); + lines.push(row.line); + } + return Object.freeze({ roots: batchRoots(roots), text: lines.join(""), lines: lines.length }); } /** Tokens the model did not ingest because a hit denied the read: bytes/4 diff --git a/integrations/claude-code-plugin/lib/tmp-state.mjs b/integrations/claude-code-plugin/lib/tmp-state.mjs index 07433b5270..164ad79324 100644 --- a/integrations/claude-code-plugin/lib/tmp-state.mjs +++ b/integrations/claude-code-plugin/lib/tmp-state.mjs @@ -159,10 +159,10 @@ export function writeCursor(file, n) { } export const STALE_STATE_MS = 24 * 60 * 60 * 1000; -// tmpStatePath's names but the code shunt's decision log (a log the user reads, not -// session state), and the recalled-key and code-trace stashes named by session id. +// Decision logs and unacknowledged shunt queues/claims retain their evidence. +// Other tmpStatePath names and recalled-key/code-trace stashes expire. const STATE_FILE_RE = - /^loom-(?!shunt-log-)(?:[a-z0-9-]+-[0-9a-f]{12}(?:\.[a-z]+)?|(?:recalled|code-trace)-[\w-]+\.json)$/; + /^loom-(?!shunt-(?:log|pending|protected|claim)-)(?:[a-z0-9-]+-[0-9a-f]{12}(?:\.[a-z]+)?|(?:recalled|code-trace)-[\w-]+\.json)$/; /** Remove this user's state files in `tmp` untouched for `maxAgeMs`: a session's * journal and cursors outlive it, since SessionEnd hooks run in parallel and none diff --git a/integrations/claude-code-plugin/tests/agent-mode-fallback.test.mjs b/integrations/claude-code-plugin/tests/agent-mode-fallback.test.mjs index 10dff4d39d..dc99890d2f 100644 --- a/integrations/claude-code-plugin/tests/agent-mode-fallback.test.mjs +++ b/integrations/claude-code-plugin/tests/agent-mode-fallback.test.mjs @@ -3,6 +3,7 @@ // person saved on this machine, nor sign a new identity up. Both run as the // host runs them, against a server whose token endpoint is away. import "./temp-home.mjs"; +import { establishFixtureSession } from "./established-session.mjs"; import { test } from "node:test"; import assert from "node:assert/strict"; import { spawn } from "node:child_process"; @@ -36,8 +37,10 @@ function run(rel, url, stdin = "") { fs.writeFileSync(path.join(home, ".loom", "config.json"), JSON.stringify({ token: PERSONAL, url }), { mode: 0o600, }); + const args = rel.startsWith("hooks/") ? ["SessionStart"] : []; + if (rel.startsWith("hooks/")) establishFixtureSession(home, "agent-fallback-session"); return new Promise((resolve) => { - const child = spawn(process.execPath, [entry(rel)], { + const child = spawn(process.execPath, [entry(rel), ...args], { cwd: home, env: hostEnv({ PATH: process.env.PATH, @@ -58,7 +61,7 @@ function run(rel, url, stdin = "") { } for (const [name, rel, stdin] of [ - ["the SessionStart hook", "hooks/loom-session-start.mjs", "{}"], + ["the SessionStart hook", "hooks/loom-session-start.mjs", JSON.stringify({ session_id: "agent-fallback-session" })], ["the MCP headers helper", "bin/loom-mcp-headers.mjs", ""], ]) { test(`${name} sends neither the person's saved token nor a sign-up when the exchange fails`, async () => { diff --git a/integrations/claude-code-plugin/tests/code-hooks-failopen.test.mjs b/integrations/claude-code-plugin/tests/code-hooks-failopen.test.mjs index 7e8e83ff39..5579c777aa 100644 --- a/integrations/claude-code-plugin/tests/code-hooks-failopen.test.mjs +++ b/integrations/claude-code-plugin/tests/code-hooks-failopen.test.mjs @@ -4,6 +4,7 @@ // Every case exits 0 within its bound and says nothing, or only what local // policy decides without the server. import "./temp-home.mjs"; +import { establishFixtureSession } from "./established-session.mjs"; import { test } from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; @@ -35,8 +36,10 @@ function checkout() { return dir; } -function freshTmp() { - return fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "loom-failopen-tmp-"))); +function freshTmp(existingSessions = []) { + const tmp = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "loom-failopen-tmp-"))); + for (const sessionId of existingSessions) establishFixtureSession(tmp, sessionId); + return tmp; } function seedIndexed(root, tmp) { @@ -50,7 +53,14 @@ function seedIndexed(root, tmp) { function run(hook, stdin, { tmp, url = REFUSED, token = "t", env = {} }) { return new Promise((resolve) => { const started = Date.now(); - const child = spawn(process.execPath, [path.join(HOOKS, hook), "X"], { + const event = { + "loom-code-shunt.mjs": "PreToolUse", + "loom-file-context.mjs": "PreToolUse", + "loom-blast-radius.mjs": "PostToolUse", + "loom-test-gate.mjs": "Stop", + "loom-validate-gate.mjs": "Stop", + }[hook]; + const child = spawn(process.execPath, [path.join(HOOKS, hook), event], { env: hostEnv({ PATH: process.env.PATH, HOME: tmp, @@ -138,7 +148,7 @@ test("malformed, empty and non-object stdin: every code hook exits 0 and prints test("no token: nothing is asked of the server and nothing is printed", async () => { const dir = checkout(); await withServer(200, async (url, seen) => { - const tmp = freshTmp(); + const tmp = freshTmp(["s"]); const read = { tool_name: "Read", tool_input: { file_path: path.join(dir, "src/a.cpp") }, @@ -168,7 +178,7 @@ test("no token: nothing is asked of the server and nothing is printed", async () test("shunt: a refused connection with no cached entry passes and is retried soon, not cached as not-indexed", async () => { const dir = checkout(); - const tmp = freshTmp(); + const tmp = freshTmp(["s"]); const r = await run( "loom-code-shunt.mjs", { tool_name: "Read", tool_input: { file_path: path.join(dir, "src/a.cpp") }, cwd: dir, session_id: "s" }, @@ -184,7 +194,7 @@ test("shunt: a refused connection with no cached entry passes and is retried soo test("shunt: a 401 from the repository lookup passes the call and records it unresolved", async () => { const dir = checkout(); await withServer(401, async (url, seen) => { - const tmp = freshTmp(); + const tmp = freshTmp(["s"]); const r = await run( "loom-code-shunt.mjs", { tool_name: "Read", tool_input: { file_path: path.join(dir, "src/a.cpp") }, cwd: dir, session_id: "s" }, @@ -200,7 +210,7 @@ test("shunt: a 401 from the repository lookup passes the call and records it unr test("shunt: a server that never answers holds the call no longer than the lookup bound", async () => { const dir = checkout(); await withServer(null, async (url) => { - const tmp = freshTmp(); + const tmp = freshTmp(["s"]); const r = await run( "loom-code-shunt.mjs", { tool_name: "Read", tool_input: { file_path: path.join(dir, "src/a.cpp") }, cwd: dir, session_id: "s" }, @@ -215,7 +225,7 @@ test("shunt: a server that never answers holds the call no longer than the looku test("shunt: an indexed entry decides locally; a symbol grep whose answer hangs keeps the plain redirect", async () => { const dir = checkout(); await withServer(null, async (url, seen, arrived) => { - const tmp = freshTmp(); + const tmp = freshTmp(["s"]); seedIndexed(dir, tmp); const r = await run( "loom-code-shunt.mjs", @@ -249,7 +259,7 @@ test("shunt: an indexed entry decides locally; a symbol grep whose answer hangs }); test("shunt: outside a git checkout, or a tool it does not judge, nothing happens", async () => { - const tmp = freshTmp(); + const tmp = freshTmp(["s"]); const r = await run( "loom-code-shunt.mjs", { tool_name: "Read", tool_input: { file_path: path.join(tmp, "x") }, cwd: tmp, session_id: "s" }, @@ -263,10 +273,10 @@ test("shunt: outside a git checkout, or a tool it does not judge, nothing happen }); /** A dirty indexed checkout and a transcript whose Edit named the file. */ -function dirtySession() { +function dirtySession(existingSessions) { const dir = checkout(); fs.appendFileSync(path.join(dir, "src/a.cpp"), "int g() { return 1; }\n"); - const tmp = freshTmp(); + const tmp = freshTmp(existingSessions); seedIndexed(dir, tmp); const transcript = path.join(tmp, "t.jsonl"); fs.writeFileSync( @@ -292,7 +302,7 @@ test("Stop gates: 401 and a hanging server both let the stop through", async () // A request can fail before the server observes it; require a witnessed run. let r; for (let attempt = 0; attempt < 3; attempt++) { - const { dir, tmp, transcript } = dirtySession(); + const { dir, tmp, transcript } = dirtySession(["s"]); arrived.delete(op); r = await run( hook, @@ -319,7 +329,7 @@ test("Stop gates: 401 and a hanging server both let the stop through", async () test("Stop gates: a Stop the gate itself caused never blocks again", async () => { await withServer(200, async (url, seen) => { for (const hook of ["loom-test-gate.mjs", "loom-validate-gate.mjs"]) { - const { dir, tmp, transcript } = dirtySession(); + const { dir, tmp, transcript } = dirtySession(["s"]); const r = await run( hook, { cwd: dir, session_id: "s", transcript_path: transcript, stop_hook_active: true }, @@ -342,7 +352,7 @@ test("blast radius and file context: 401 and a hanging server add nothing to the // A request can fail before the server observes it; require a witnessed run. let r; for (let attempt = 0; attempt < 3; attempt++) { - const { dir, tmp } = dirtySession(); + const { dir, tmp } = dirtySession([`s-${hook}-${attempt}`]); const edit = { tool_name: "Edit", tool_input: { file_path: path.join(dir, "src/a.cpp") }, @@ -373,7 +383,7 @@ test("blast radius and file context: 401 and a hanging server add nothing to the test("shunt: the whole hook ends by its deadline even when one answer is slower than it", async () => { const dir = checkout(); await withServer(null, async (url, seen) => { - const tmp = freshTmp(); + const tmp = freshTmp(["s"]); seedIndexed(dir, tmp); const r = await run( "loom-code-shunt.mjs", @@ -389,7 +399,7 @@ test("shunt: the whole hook ends by its deadline even when one answer is slower test("shunt: a tracker search reaches tracker.duplicate_check redacted", async () => { const dir = checkout(); - const tmp = freshTmp(); + const tmp = freshTmp(["s"]); seedIndexed(dir, tmp); const bodies = await withCapturingServer(async (url) => { const command = 'gh issue list --search "login fails password=hunter2"'; @@ -405,7 +415,7 @@ test("shunt: a tracker search reaches tracker.duplicate_check redacted", async ( }); test("blast radius and test gate: only the diff's paths and hunk headers reach the server", async () => { - const { dir, tmp, transcript } = dirtySession(); + const { dir, tmp, transcript } = dirtySession(["s"]); fs.appendFileSync(path.join(dir, "src/a.cpp"), 'const char* api_key = "s3cr3t-value";\n'); // a second change three lines above the first: two hunks unless they are merged const src = path.join(dir, "src/a.cpp"); @@ -454,7 +464,7 @@ test("blast radius and test gate: only the diff's paths and hunk headers reach t }); test("blast radius: a change with no path or range for the server sends nothing", async () => { - const { dir, tmp } = dirtySession(); + const { dir, tmp } = dirtySession(["s"]); execFileSync("git", ["checkout", "-q", "--", "src/a.cpp"], { cwd: dir }); fs.chmodSync(path.join(dir, "src/a.cpp"), 0o755); const bodies = await withCapturingServer(async (url) => { diff --git a/integrations/claude-code-plugin/tests/code-shunt-hook.test.mjs b/integrations/claude-code-plugin/tests/code-shunt-hook.test.mjs index bcfadedaad..695319765e 100644 --- a/integrations/claude-code-plugin/tests/code-shunt-hook.test.mjs +++ b/integrations/claude-code-plugin/tests/code-shunt-hook.test.mjs @@ -3,6 +3,7 @@ // seeded in the shared cache (no code.repos call), the hook run as Claude // Code runs it, and a local server only where a call is expected. import "./temp-home.mjs"; +import { establishFixtureSession } from "./established-session.mjs"; import { test } from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; @@ -13,6 +14,7 @@ import { execFileSync, spawn } from "node:child_process"; import { fileURLToPath } from "node:url"; import { cachePathFor } from "../lib/repo-scope.mjs"; import { batchKind } from "../lib/shunt-ledger.mjs"; +import { protectSession, sessionIsProtected } from "../lib/session-protection.mjs"; import { sessionStatePath, tmpStatePath } from "../lib/tmp-state.mjs"; import { hostEnv } from "./host-env.mjs"; @@ -45,7 +47,7 @@ function checkout(files) { /** A fresh TMPDIR holding the repo entry the session-start primer would have * written. */ -function seed(root, { asOf = FAR_BEHIND } = {}) { +function seed(root, { asOf = FAR_BEHIND, existingSessions = [] } = {}) { const tmp = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "loom-shunt-tmp-"))); fs.writeFileSync( cachePathFor(root, tmp), @@ -61,6 +63,7 @@ function seed(root, { asOf = FAR_BEHIND } = {}) { fetchedAt: "", }), ); + for (const sessionId of existingSessions) establishFixtureSession(tmp, sessionId); return tmp; } @@ -126,7 +129,7 @@ const readOf = (dir, rel) => ({ test("a checkout far behind the index is still shunted", async () => { const { dir } = checkout({ "src/a.cpp": BIG }); - const r = await runHook(readOf(dir, "src/a.cpp"), { tmp: seed(dir) }); + const r = await runHook(readOf(dir, "src/a.cpp"), { tmp: seed(dir, { existingSessions: ["e2e"] }) }); assert.match( r.deny, /^loom-code-shunt: src\/a\.cpp is 900 lines and is in loom's code index \(as of 0123456789ab\)\./, @@ -137,7 +140,7 @@ test("a checkout far behind the index is still shunted", async () => { test("local_first stands the shunt down entirely, even when HEAD is the indexed commit", async () => { const { dir, head } = checkout({ "src/a.cpp": BIG }); for (const env of [{ LOOM_CODE_LOCAL_FIRST: "on" }, { CLAUDE_PLUGIN_OPTION_LOCAL_FIRST: "true" }]) { - const tmp = seed(dir, { asOf: head }); + const tmp = seed(dir, { asOf: head, existingSessions: ["e2e"] }); const r = await runHook(readOf(dir, "src/a.cpp"), { tmp, env }); assert.equal(r.deny, null); assert.equal(r.log, "", "no decision at all"); @@ -152,7 +155,7 @@ test("local_first stands the shunt down entirely, even when HEAD is the indexed test("a modified file still passes: the index cannot describe uncommitted edits", async () => { const { dir } = checkout({ "src/a.cpp": BIG }); fs.appendFileSync(path.join(dir, "src/a.cpp"), "// mine\n"); - const r = await runHook(readOf(dir, "src/a.cpp"), { tmp: seed(dir) }); + const r = await runHook(readOf(dir, "src/a.cpp"), { tmp: seed(dir, { existingSessions: ["e2e"] }) }); assert.equal(r.deny, null); assert.match(r.log, /\tpass\tRead\t\d+\t\tmodified\t/); }); @@ -166,7 +169,7 @@ test("a tree-wide grep for matching lines runs: it costs less than its redirect" cwd: dir, session_id: "e2e", }, - { tmp: seed(dir) }, + { tmp: seed(dir, { existingSessions: ["e2e"] }) }, ); assert.equal(r.deny, null); assert.match(r.log, /\tpass\tGrep\t\d+\t\ttree_content_grep\t/); @@ -182,7 +185,7 @@ test("a tree-wide grep that lists files (Claude Code's default) passes", async ( cwd: dir, session_id: `e2e-${output_mode}`, }, - { tmp: seed(dir) }, + { tmp: seed(dir, { existingSessions: [`e2e-${output_mode}`] }) }, ); assert.equal(r.deny, null, String(output_mode)); assert.match(r.log, /\tpass\tGrep\t\d+\t\tfile_list_grep\t/); @@ -196,7 +199,7 @@ test("a one-file grep asking for line numbers or context runs as content", async const tool_input = { pattern: "busy_timeout|max_data_size", path: file, ...flags }; const r = await runHook( { tool_name: "Grep", tool_input, cwd: dir, session_id: `lines-${Object.keys(flags)[0]}` }, - { tmp: seed(dir) }, + { tmp: seed(dir, { existingSessions: [`lines-${Object.keys(flags)[0]}`] }) }, ); assert.equal(r.deny, null); assert.deepEqual(r.updatedInput, { ...tool_input, output_mode: "content" }); @@ -207,7 +210,10 @@ test("a one-file grep asking for line numbers or context runs as content", async { pattern: "busy_timeout", path: file, "-n": true, output_mode: "count" }, { pattern: "busy_timeout", path: path.join(dir, "src"), "-n": true }, ]) { - const r = await runHook({ tool_name: "Grep", tool_input, cwd: dir, session_id: "lines-no" }, { tmp: seed(dir) }); + const r = await runHook( + { tool_name: "Grep", tool_input, cwd: dir, session_id: "lines-no" }, + { tmp: seed(dir, { existingSessions: ["lines-no"] }) }, + ); assert.equal(r.updatedInput, null, JSON.stringify(tool_input)); } }); @@ -234,7 +240,7 @@ test("a symbol grep is answered by the index; a named file you modified gets its cwd: dir, session_id: "e2e", }; - const r = await withServer(answers, (url) => runHook(cmd, { tmp: seed(dir), url })); + const r = await withServer(answers, (url) => runHook(cmd, { tmp: seed(dir, { existingSessions: ["e2e"] }), url })); assert.match( r.deny, /Index answer: DB::MergeTreeData::renameTempPartAndAdd is defined at src\/a\.cpp:7327-7344 \(code-x @ 012345678, scip tier\)\./, @@ -248,7 +254,7 @@ test("a symbol grep is answered by the index; a named file you modified gets its test("a chained Bash read is judged segment by segment; the first deny wins", async () => { const { dir } = checkout({ "src/a.cpp": BIG, "src/small.cpp": "int x;\n" }); - const tmp = seed(dir); + const tmp = seed(dir, { existingSessions: ["e2e"] }); const bash = (command) => ({ tool_name: "Bash", tool_input: { command }, cwd: dir, session_id: "e2e" }); // As a Git Bash command names it on Windows: forward slashes. const cdDir = dir.replace(/\\/g, "/"); @@ -258,7 +264,9 @@ test("a chained Bash read is judged segment by segment; the first deny wins", as assert.equal(lines.length, 2, "the small read passed and was counted, the big one denied"); assert.match(lines[0], /\tpass\tBash:cat\t.*\tsrc\/small\.cpp\t/); assert.match(lines[1], /\thit\tBash:cat\t/); - const p = await runHook(bash("cd src && sed -n '1,40p' a.cpp; echo ---"), { tmp: seed(dir) }); + const p = await runHook(bash("cd src && sed -n '1,40p' a.cpp; echo ---"), { + tmp: seed(dir, { existingSessions: ["e2e"] }), + }); assert.equal(p.deny, null, "a ranged read passes (no read cap set)"); assert.match(p.log, /\tpass\tBash:sed\t.*targeted_span\tsrc\/a\.cpp\t1\t40/); }); @@ -266,7 +274,7 @@ test("a chained Bash read is judged segment by segment; the first deny wins", as // The hook's pending batch for the seeded repository (o/code-x), the server at // `url`, runHook's token and a session. const pendingPath = (tmp, url, session = "e2e", namespace = "code-x") => - sessionStatePath(batchKind({ serverUrl: url, token: "t", org: "o", namespace }), session, { ext: ".log", tmp }); + sessionStatePath(batchKind({ serverUrl: url, token: "t", org: "o", namespace }), session, { ext: ".log.control", tmp }); const reporting = { LOOM_SHUNT_REPORT: "on", LOOM_SHUNT_READ_CAP: "200" }; const rangedRead = (dir) => ({ ...readOf(dir, "src/a.cpp"), @@ -280,7 +288,7 @@ const posix = process.platform !== "win32"; test("a deny claims the pending batch, posts it as code.outcome and removes it", async () => { const { dir } = checkout({ "src/a.cpp": BIG }); - const tmp = seed(dir); + const tmp = seed(dir, { existingSessions: ["e2e"] }); await withServer({}, async (url, seen) => { const r = await runHook(readOf(dir, "src/a.cpp"), { tmp, url, env: reporting }); assert.ok(r.deny); @@ -299,7 +307,7 @@ test("a deny claims the pending batch, posts it as code.outcome and removes it", test("the read cap denies a file twice per session, then passes it as cap_spent", async () => { const { dir } = checkout({ "src/a.cpp": BIG }); - const tmp = seed(dir); + const tmp = seed(dir, { existingSessions: ["e2e"] }); const env = { LOOM_SHUNT_READ_CAP: "200" }; for (let i = 0; i < 2; i++) assert.ok((await runHook(rangedRead(dir), { tmp, env })).deny, `denial ${i + 1}`); const third = await runHook(rangedRead(dir), { tmp, env }); @@ -311,7 +319,7 @@ test("symlinks planted at the shunt's tmp files are not followed and cannot lock skip: !posix, }, async () => { const { dir } = checkout({ "src/a.cpp": BIG }); - const tmp = seed(dir); + const tmp = seed(dir, { existingSessions: ["e2e"] }); const planted = {}; const victims = {}; await withServer({}, async (url, seen) => { @@ -341,7 +349,7 @@ test("symlinks planted at the shunt's tmp files are not followed and cannot lock test("a hard link planted at the pending batch is neither posted nor truncated", { skip: !posix }, async () => { const { dir } = checkout({ "src/a.cpp": BIG }); - const tmp = seed(dir); + const tmp = seed(dir, { existingSessions: ["e2e"] }); const victim = path.join(tmp, "victim-log"); fs.writeFileSync(victim, plantedBatch); await withServer({}, async (url, seen) => { @@ -357,7 +365,7 @@ test("a flush also posts the batch another session left behind", async () => { const { dir } = checkout({ "src/a.cpp": BIG }); fs.appendFileSync(path.join(dir, "src/a.cpp"), "// mine\n"); const { dir: other } = checkout({ "src/a.cpp": BIG }); - const tmp = seed(dir); + const tmp = seed(dir, { existingSessions: ["A", "B"] }); fs.writeFileSync(cachePathFor(other, tmp), fs.readFileSync(cachePathFor(dir, tmp))); await withServer({}, async (url, seen) => { const a = await runHook({ ...readOf(dir, "src/a.cpp"), session_id: "A" }, { tmp, url, env: reporting }); @@ -380,7 +388,7 @@ test("a flush also posts the batch another session left behind", async () => { test("a flush leaves another reporting scope's batch alone", async () => { const { dir } = checkout({ "src/a.cpp": BIG }); - const tmp = seed(dir); + const tmp = seed(dir, { existingSessions: ["e2e"] }); await withServer({}, async (url, seen) => { // Another repository's batch, and one this repository's lines would go to under another server. const others = [pendingPath(tmp, url, "X", "code-other"), pendingPath(tmp, "http://127.0.0.1:9", "Y")]; @@ -402,7 +410,7 @@ const plantCap = (tmp, session = "e2e") => { test("a symbol grep whose budget cannot be written passes as budget_unrecorded", { skip: !posix }, async () => { const { dir } = checkout({ "src/a.h": "bool renameTempPartAndAdd(\n", "src/a.cpp": "// def\n" }); - const tmp = seed(dir); + const tmp = seed(dir, { existingSessions: ["e2e"] }); const victim = plantCap(tmp); const cmd = { tool_name: "Bash", @@ -420,7 +428,7 @@ test("an answered tracker search whose budget cannot be written passes as budget skip: !posix, }, async () => { const { dir } = checkout({ "src/a.cpp": "// x\n" }); - const tmp = seed(dir); + const tmp = seed(dir, { existingSessions: ["e2e"] }); const victim = plantCap(tmp); const cmd = { tool_name: "Bash", @@ -444,9 +452,122 @@ test("an answered tracker search whose budget cannot be written passes as budget test("an over-cap read whose budget cannot be written passes as budget_unrecorded", { skip: !posix }, async () => { const { dir } = checkout({ "src/a.cpp": BIG }); - const tmp = seed(dir); + const tmp = seed(dir, { existingSessions: ["e2e"] }); plantCap(tmp); const r = await runHook(rangedRead(dir), { tmp, env: { LOOM_SHUNT_READ_CAP: "200" } }); assert.equal(r.deny, null); assert.match(r.log, /\tpass\tRead\t\d+\t\tbudget_unrecorded\t/); }); + + +test("a producer protected after queuing keeps its bytes while an unrelated clean hit posts", async () => { + const { dir } = checkout({ "src/a.cpp": BIG }); + fs.appendFileSync(path.join(dir, "src/a.cpp"), "// private producer change\n"); + const { dir: other } = checkout({ "src/b.cpp": BIG }); + const tmp = seed(dir, { existingSessions: ["A", "B"] }); + const rootA = establishFixtureSession(tmp, "A"); + const rootB = establishFixtureSession(tmp, "B"); + fs.writeFileSync(cachePathFor(other, tmp), fs.readFileSync(cachePathFor(dir, tmp))); + try { + await withServer({}, async (url, seen) => { + const a = await runHook({ ...readOf(dir, "src/a.cpp"), session_id: "A" }, { tmp, url, env: reporting }); + assert.equal(a.deny, null); + const queuedA = pendingPath(tmp, url, "A"); + const before = fs.readFileSync(queuedA); + assert.ok(before.length > 0); + assert.equal(outcomes(seen).length, 0); + protectSession(rootA); + assert.equal(sessionIsProtected(rootA), true); + assert.equal(sessionIsProtected(rootB), false); + const b = await runHook({ ...readOf(other, "src/b.cpp"), session_id: "B" }, { tmp, url, env: reporting }); + assert.ok(b.deny); + await settle(); + assert.equal(outcomes(seen).length, 1, "the clean producer's own hit is sent"); + assert.equal(outcomes(seen)[0].body.action, "shunt_hit"); + assert.ok(outcomes(seen)[0].body.spans.some((span) => span.startsWith("src/b.cpp:"))); + assert.equal(JSON.stringify(outcomes(seen)).includes("src/a.cpp"), false); + assert.equal(outcomes(seen).some((entry) => entry.body.source === "modified"), false); + assert.deepEqual(fs.readFileSync(queuedA), before, "protected evidence is neither claimed nor changed"); + assert.equal(fs.existsSync(pendingPath(tmp, url, "B")), false); + assert.equal(sessionIsProtected(rootB), false); + }); + } finally { + for (const owned of [tmp, dir, other]) fs.rmSync(owned, { recursive: true, force: true }); + } +}); + +test("a marked aggregate response protects every producer and retains both unacknowledged claims", async () => { + const { dir } = checkout({ "src/a.cpp": BIG }); + fs.appendFileSync(path.join(dir, "src/a.cpp"), "// producer change\n"); + const { dir: other } = checkout({ "src/b.cpp": BIG }); + const tmp = seed(dir, { existingSessions: ["A", "B"] }); + const rootA = establishFixtureSession(tmp, "A"); + const rootB = establishFixtureSession(tmp, "B"); + fs.writeFileSync(cachePathFor(other, tmp), fs.readFileSync(cachePathFor(dir, tmp))); + try { + await withServer({ "/v1/code.outcome": { mirrored: true } }, async (url, seen) => { + const a = await runHook({ ...readOf(dir, "src/a.cpp"), session_id: "A" }, { tmp, url, env: reporting }); + assert.equal(a.deny, null); + const queuedA = pendingPath(tmp, url, "A"); + const beforeA = fs.readFileSync(queuedA, "utf8"); + assert.equal(outcomes(seen).length, 0); + assert.equal(sessionIsProtected(rootA), false); + assert.equal(sessionIsProtected(rootB), false); + const b = await runHook({ ...readOf(other, "src/b.cpp"), session_id: "B" }, { tmp, url, env: reporting }); + assert.ok(b.deny); + await settle(); + assert.equal(outcomes(seen).length, 1, "the first marked answer suppresses the aggregate's second body"); + assert.equal(outcomes(seen)[0].body.action, "shunt_hit"); + assert.equal(sessionIsProtected(rootA), true); + assert.equal(sessionIsProtected(rootB), true); + assert.equal(fs.existsSync(queuedA), false); + assert.equal(fs.existsSync(pendingPath(tmp, url, "B")), false); + const claims = fs.readdirSync(tmp).filter((name) => name.startsWith("loom-shunt-protected-claim-")); + assert.equal(claims.length, 2, "claiming moves evidence; a marked answer does not acknowledge it"); + const retained = claims.map((name) => fs.readFileSync(path.join(tmp, name), "utf8")); + assert.ok(retained.includes(beforeA), "producer A's exact queued bytes remain in its claim"); + const rows = retained.flatMap((text) => text.trimEnd().split("\n").map((line) => JSON.parse(line))); + assert.equal(rows.length, 2); + const rowA = rows.find((row) => + row.protectionRoots.some((root) => root.base === rootA.base && root.id === rootA.id), + ); + const rowB = rows.find((row) => + row.protectionRoots.some((root) => root.base === rootB.base && root.id === rootB.id), + ); + assert.ok(rowA && rowB, "both actual producer roots stay attached to their evidence"); + assert.match(rowA.line, /\tpass\tRead\t.*\tmodified\tsrc\/a\.cpp\t/); + assert.match(rowB.line, /\thit\tRead\t.*\tsrc\/b\.cpp\t/); + const scopePrefix = `loom-${batchKind({ serverUrl: url, token: "t", org: "o", namespace: "code-x" })}-`; + assert.deepEqual( + fs.readdirSync(tmp).filter((name) => name.startsWith(scopePrefix) && name.endsWith(".log.control")), + [], + ); + }); + } finally { + for (const owned of [tmp, dir, other]) fs.rmSync(owned, { recursive: true, force: true }); + } +}); + +test("an unattributed batch in the same protected scope stays untouched while a clean hit posts", async () => { + const { dir } = checkout({ "src/b.cpp": BIG }); + const tmp = seed(dir, { existingSessions: ["B"] }); + try { + await withServer({}, async (url, seen) => { + const unattributed = pendingPath(tmp, url, "unknown-producer"); + fs.writeFileSync(unattributed, plantedBatch, { mode: 0o600 }); + const before = fs.readFileSync(unattributed); + const b = await runHook({ ...readOf(dir, "src/b.cpp"), session_id: "B" }, { tmp, url, env: reporting }); + assert.ok(b.deny); + await settle(); + assert.equal(outcomes(seen).length, 1); + assert.equal(outcomes(seen)[0].body.action, "shunt_hit"); + assert.ok(outcomes(seen)[0].body.spans.some((span) => span.startsWith("src/b.cpp:"))); + assert.equal(carriesPlanted(seen), false); + assert.deepEqual(fs.readFileSync(unattributed), before, "a matching filename cannot supply producer authority"); + assert.equal(fs.existsSync(pendingPath(tmp, url, "B")), false); + assert.deepEqual(fs.readdirSync(tmp).filter((name) => name.startsWith("loom-shunt-protected-claim-")), []); + }); + } finally { + for (const owned of [tmp, dir]) fs.rmSync(owned, { recursive: true, force: true }); + } +}); diff --git a/integrations/claude-code-plugin/tests/established-session.mjs b/integrations/claude-code-plugin/tests/established-session.mjs new file mode 100644 index 0000000000..28ab0e5d5e --- /dev/null +++ b/integrations/claude-code-plugin/tests/established-session.mjs @@ -0,0 +1,17 @@ +// Explicit fixture input for a host identity established before hook delivery. +import fs from "node:fs"; +import path from "node:path"; +import { + bindProtectionAlias, + createProtectionRoot, + findProtectionAlias, +} from "../lib/session-protection.mjs"; + +export function establishFixtureSession(home, sessionId, host = "claude-code") { + const base = path.join(home, ".loom", "session-protection", "v1"); + const existing = fs.existsSync(base) ? findProtectionAlias(host, sessionId, { base }) : undefined; + if (existing) return existing; + const root = createProtectionRoot({ base }); + bindProtectionAlias(root, host, sessionId); + return root; +} diff --git a/integrations/claude-code-plugin/tests/file-rules-team.test.mjs b/integrations/claude-code-plugin/tests/file-rules-team.test.mjs index 6e28861237..de7c1e1dbf 100644 --- a/integrations/claude-code-plugin/tests/file-rules-team.test.mjs +++ b/integrations/claude-code-plugin/tests/file-rules-team.test.mjs @@ -1,6 +1,7 @@ // The file-context hook reads the repository's primary code // namespace (team file rules) beside the person's own, in one request. import "./temp-home.mjs"; +import { establishFixtureSession } from "./established-session.mjs"; import { test } from "node:test"; import assert from "node:assert/strict"; import { execFileSync, spawn } from "node:child_process"; @@ -138,6 +139,7 @@ async function runFileContextHook(rendered) { const hook = fileURLToPath(new URL("../hooks/loom-file-context.mjs", import.meta.url)); const dir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "loom-fc-hook-"))); const tmp = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "loom-fc-tmp-"))); + establishFixtureSession(tmp, "s"); fs.mkdirSync(path.join(dir, "src")); fs.writeFileSync(path.join(dir, "src/a.cpp"), "int x;\n"); const g = (...a) => execFileSync("git", ["-c", "user.email=t@t", "-c", "user.name=t", ...a], { cwd: dir }); @@ -161,7 +163,7 @@ async function runFileContextHook(rendered) { await new Promise((r) => srv.listen(0, "127.0.0.1", r)); try { const out = await new Promise((resolve) => { - const child = spawn(process.execPath, [hook], { + const child = spawn(process.execPath, [hook, "PreToolUse"], { cwd: dir, env: hostEnv({ PATH: process.env.PATH, diff --git a/integrations/claude-code-plugin/tests/flush-hook.test.mjs b/integrations/claude-code-plugin/tests/flush-hook.test.mjs index df0c753b4a..0a831f495e 100644 --- a/integrations/claude-code-plugin/tests/flush-hook.test.mjs +++ b/integrations/claude-code-plugin/tests/flush-hook.test.mjs @@ -1,6 +1,7 @@ // The detached flusher keeps its cursor in a shared tmp dir: a symlink planted // at the cursor path is refused, and the file it points to stays as it was. import "./temp-home.mjs"; +import { establishFixtureSession } from "./established-session.mjs"; import { test } from "node:test"; import assert from "node:assert/strict"; import { spawn } from "node:child_process"; @@ -43,11 +44,14 @@ const posix = process.platform !== "win32"; test("the flusher advances a cursor it owns and never follows a symlink at the cursor path", { skip: !posix, }, async () => { - const { srv, url } = await server(); const dir = fs.mkdtempSync(path.join(os.tmpdir(), "loom-flushhook-")); + const root = establishFixtureSession(dir, "append-producer"); + const { srv, url } = await server(); try { const own = path.join(dir, "loom-conv-a.offset"); - const jobPath = await flush(dir, url, { appends: [append], cursorPath: own, advanceTo: 42 }); + const jobPath = await flush(dir, url, { + protectionRoots: [root], appends: [append], cursorPath: own, advanceTo: 42, + }); assert.equal(fs.readFileSync(own, "utf8"), "42"); assert.equal(fs.existsSync(jobPath), false); @@ -55,7 +59,9 @@ test("the flusher advances a cursor it owns and never follows a symlink at the c fs.writeFileSync(victim, "keep"); const planted = path.join(dir, "loom-conv-b.offset"); fs.symlinkSync(victim, planted); - const plantedJob = await flush(dir, url, { appends: [append], cursorPath: planted, advanceTo: 42 }); + const plantedJob = await flush(dir, url, { + protectionRoots: [root], appends: [append], cursorPath: planted, advanceTo: 42, + }); assert.equal(fs.readFileSync(victim, "utf8"), "keep"); assert.equal(fs.existsSync(plantedJob), false); assert.ok(fs.lstatSync(planted).isSymbolicLink()); @@ -66,9 +72,11 @@ test("the flusher advances a cursor it owns and never follows a symlink at the c }); test("a failed post rolls back a cursor it owns and never a symlink at the cursor path", { skip: !posix }, async () => { - const { srv, url } = await server(500); const dir = fs.mkdtempSync(path.join(os.tmpdir(), "loom-flushhook-")); + const root = establishFixtureSession(dir, "post-producer"); + const { srv, url } = await server(500); const job = (cursorPath) => ({ + protectionRoots: [root], posts: [{ op: "memory.set", body: { k: 1 } }], cursorPath, advanceTo: 42, diff --git a/integrations/claude-code-plugin/tests/flush-job-protection.test.mjs b/integrations/claude-code-plugin/tests/flush-job-protection.test.mjs new file mode 100644 index 0000000000..b105c10586 --- /dev/null +++ b/integrations/claude-code-plugin/tests/flush-job-protection.test.mjs @@ -0,0 +1,249 @@ +import "./temp-home.mjs"; +import assert from "node:assert/strict"; +import { EventEmitter } from "node:events"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { afterEach, test } from "node:test"; + +import { dispatchFlush, runFlushJob } from "../lib/flush-job.mjs"; +import { requestJson } from "../lib/loom-client.mjs"; +import { createProtectionRoot, protectSession, sessionIsProtected } from "../lib/session-protection.mjs"; +import { + sessionProtectionContext, + withAutomaticSessionProtection, + withSessionProtection, +} from "../lib/session-response.mjs"; + +const owned = new Set(); +function fixture() { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), "loom-flush-protection-")); + owned.add(directory); + return { directory, root: createProtectionRoot({ base: directory }) }; +} +afterEach(() => { + for (const directory of owned) fs.rmSync(directory, { recursive: true, force: true }); + owned.clear(); +}); + +function cursor() { + return { + value: "10", + read() { + return this.value; + }, + write(_path, value) { + this.value = String(value); + }, + }; +} + +if (process.platform === "win32" || typeof process.getuid !== "function") { + test("automatic contexts refuse unavailable native capsule authority", () => { + let ran = false; + withAutomaticSessionProtection({ base: path.resolve("missing"), id: "00000000-0000-0000-0000-000000000000" }, () => { + ran = true; + }); + assert.equal(ran, false); + }); +} else { +test("unknown and protected producers leave no queued content or child", () => { + const { directory, root } = fixture(); + const before = fs.readdirSync(directory); + let spawns = 0; + const options = { + tmp: directory, + spawnImpl() { + spawns++; + throw new Error("must not spawn"); + }, + }; + assert.equal(dispatchFlush({ posts: [{ op: "memory.set_from_messages", body: {} }] }, options), false); + protectSession(root); + assert.equal( + withSessionProtection(root, () => dispatchFlush({ appends: [{}] }, options)), + false, + ); + assert.equal(spawns, 0); + assert.deepEqual(fs.readdirSync(directory), before); +}); + +test("a queued job captures its producer root rather than a supplied replacement", () => { + const { directory, root } = fixture(); + const replacement = createProtectionRoot({ base: directory }); + let job; + const options = { + tmp: directory, + spawnImpl(_command, args) { + job = JSON.parse(fs.readFileSync(args[1], "utf8")); + const child = new EventEmitter(); + child.unref = () => {}; + return child; + }, + }; + assert.equal( + withAutomaticSessionProtection(root, () => + dispatchFlush({ protectionRoots: [replacement], appends: [{}] }, options), + ), + true, + ); + assert.deepEqual(job.protectionRoots, [root]); + protectSession(root); + assert.equal(sessionIsProtected(replacement), false); +}); + +test("late protection discards a detached job despite a different clean caller", async () => { + const { root, directory } = fixture(); + const replacement = createProtectionRoot({ base: directory }); + const job = { protectionRoots: [root], appends: [{}], cursorPath: "/cursor", advanceTo: 99 }; + protectSession(root); + let sends = 0; + const position = cursor(); + const result = await withSessionProtection(replacement, () => + runFlushJob(job, { + post: async () => { + sends++; + return { stored: true }; + }, + cursor: position, + }), + ); + assert.deepEqual(result, { postsOk: false, appendsOk: false }); + assert.equal(sends, 0); + assert.equal(position.value, "10"); +}); + +test("a marked post prevents the append phase and success cursor", async () => { + const { root } = fixture(); + const position = cursor(); + const sends = []; + let notes = 0; + const result = await runFlushJob( + { + protectionRoots: [root], + posts: [{ op: "session.brief_save", body: {} }], + appends: [{}], + cursorPath: "/cursor", + advanceTo: 99, + }, + { + async post(op, body) { + sends.push(op); + assert.equal(sessionProtectionContext().automatic, true); + return ( + await requestJson(`http://server/v1/${op}`, { + body, + fetchImpl: async () => ({ status: 200, json: async () => ({ mirrored: true }) }), + }) + ).body; + }, + cursor: position, + noteRecording() { + notes++; + }, + }, + ); + assert.deepEqual(sends, ["session.brief_save"]); + assert.deepEqual(result, { postsOk: false, appendsOk: false }); + assert.equal(position.value, "10"); + assert.equal(notes, 0); + assert.equal(sessionIsProtected(root), true); +}); + +test("each dispatch rechecks protection even within the same parallel phase", async () => { + const { root } = fixture(); + let sends = 0; + const result = await runFlushJob( + { + protectionRoots: [root], + posts: [ + { op: "session.brief_save", body: {} }, + { op: "memory.set_from_messages", body: {} }, + ], + }, + { + async post() { + sends++; + protectSession(root); + return { stored: true }; + }, + cursor: cursor(), + }, + ); + assert.equal(sends, 1); + assert.deepEqual(result, { postsOk: false, appendsOk: false }); +}); + +test("a damaged queued root refuses delivery and a protected final reply never advances", async () => { + const { root, directory } = fixture(); + const position = cursor(); + let sends = 0; + const job = { protectionRoots: [root], appends: [{}], cursorPath: "/cursor", advanceTo: 99 }; + const io = { + async post() { + sends++; + protectSession(root); + return { stored: true }; + }, + cursor: position, + }; + assert.equal((await runFlushJob(job, io)).appendsOk, false); + assert.equal(sends, 1); + assert.equal(position.value, "10"); + fs.unlinkSync(path.join(directory, root.id, "state")); + await runFlushJob(job, io); + await runFlushJob({ appends: [{}] }, io); + assert.equal(sends, 1); +}); + +test("queued root identity cannot switch across an awaited post", async () => { + const { root, directory } = fixture(); + const replacement = createProtectionRoot({ base: directory }); + const reference = { ...root }; + const position = cursor(); + let sends = 0; + const result = await runFlushJob( + { + protectionRoots: [reference], + posts: [{ op: "session.brief_save", body: {} }], + appends: [{}], + cursorPath: "/cursor", + advanceTo: 99, + }, + { + async post() { + sends++; + protectSession(root); + Object.assign(reference, replacement); + await Promise.resolve(); + return { stored: true }; + }, + cursor: position, + }, + ); + assert.equal(sends, 1); + assert.equal(position.value, "10"); + assert.deepEqual(result, { postsOk: false, appendsOk: false }); +}); + +test("suppressed optimistic jobs roll back rather than acknowledging unsent posts", async () => { + const { root } = fixture(); + protectSession(root); + for (const protectionRoots of [undefined, [root]]) { + const position = cursor(); + position.value = "99"; + let sends = 0; + const result = await runFlushJob( + { protectionRoots, posts: [{ op: "session.brief_save", body: {} }], cursorPath: "/cursor", advanceTo: 99, rollbackTo: 10 }, + { + post: async () => { sends++; return { stored: true }; }, + cursor: position, + }, + ); + assert.deepEqual(result, { postsOk: false, appendsOk: false }); + assert.equal(sends, 0); + assert.equal(position.value, "10"); + } +}); + +} diff --git a/integrations/claude-code-plugin/tests/flush-job.test.mjs b/integrations/claude-code-plugin/tests/flush-job.test.mjs index 4a9feb0afc..54db9a09e8 100644 --- a/integrations/claude-code-plugin/tests/flush-job.test.mjs +++ b/integrations/claude-code-plugin/tests/flush-job.test.mjs @@ -1,4 +1,5 @@ import "./temp-home.mjs"; +import { establishFixtureSession } from "./established-session.mjs"; import { test } from "node:test"; import assert from "node:assert/strict"; import { EventEmitter } from "node:events"; @@ -6,6 +7,7 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { dispatchFlush, flushJobPath, runFlushJob } from "../lib/flush-job.mjs"; +import { withAutomaticSessionProtection } from "../lib/session-response.mjs"; const tmp = () => fs.mkdtempSync(path.join(os.tmpdir(), "loom-flushjob-")); @@ -20,6 +22,7 @@ test("job files carry the name the client status check counts", () => { test("dispatchFlush writes the job and spawns the flusher detached with the request bound", () => { const dir = tmp(); + const root = establishFixtureSession(os.homedir(), "dispatch"); const calls = []; const spawnImpl = (cmd, args, opts) => { calls.push({ cmd, args, opts }); @@ -27,31 +30,45 @@ test("dispatchFlush writes the job and spawns the flusher detached with the requ child.unref = () => {}; return child; }; - assert.equal(dispatchFlush({ posts: [{ op: "x", body: {} }] }, { timeoutMs: 1234, spawnImpl, tmp: dir }), true); + assert.equal( + withAutomaticSessionProtection(root, () => + dispatchFlush({ posts: [{ op: "x", body: {} }] }, { timeoutMs: 1234, spawnImpl, tmp: dir }), + ), + true, + ); assert.equal(calls.length, 1); assert.match(calls[0].args[0], /hooks[\\/]loom-flush\.mjs$/); assert.equal(calls[0].opts.detached, true); assert.equal(calls[0].opts.windowsHide, true); assert.equal(calls[0].opts.env.LOOM_HOOK_TIMEOUT_MS, "1234"); - const job = JSON.parse(fs.readFileSync(calls[0].args[1], "utf8")); + const { protectionRoots, ...job } = JSON.parse(fs.readFileSync(calls[0].args[1], "utf8")); + assert.deepEqual(protectionRoots, [root]); assert.deepEqual(job, { posts: [{ op: "x", body: {} }] }); }); test("a spawn that throws leaves no job behind; a later spawn error does not crash the hook", () => { const dir = tmp(); + const root = establishFixtureSession(os.homedir(), "spawn-errors"); const throwing = () => { throw new Error("EAGAIN"); }; - assert.equal(dispatchFlush({ posts: [] }, { spawnImpl: throwing, tmp: dir }), false); + assert.equal( + withAutomaticSessionProtection(root, () => dispatchFlush({ posts: [] }, { spawnImpl: throwing, tmp: dir })), + false, + ); assert.deepEqual(fs.readdirSync(dir), []); const child = new EventEmitter(); child.unref = () => {}; - assert.equal(dispatchFlush({ posts: [] }, { spawnImpl: () => child, tmp: dir }), true); + assert.equal( + withAutomaticSessionProtection(root, () => dispatchFlush({ posts: [] }, { spawnImpl: () => child, tmp: dir })), + true, + ); assert.doesNotThrow(() => child.emit("error", new Error("spawn failed"))); }); test("appends advance the cursor only when every one landed", async () => { - const job = { appends: [{ msg_id: "a" }, { msg_id: "b" }], cursorPath: "/c", advanceTo: 99 }; + const root = establishFixtureSession(os.homedir(), "append-job"); + const job = { protectionRoots: [root], appends: [{ msg_id: "a" }, { msg_id: "b" }], cursorPath: "/c", advanceTo: 99 }; const ok = memCursor({ "/c": "10" }); assert.deepEqual(await runFlushJob(job, { post: async () => ({ stored: true }), cursor: ok }), { postsOk: false, @@ -69,8 +86,9 @@ test("appends advance the cursor only when every one landed", async () => { test("the replies reach the recording note", async () => { const seen = []; + const root = establishFixtureSession(os.homedir(), "recording-job"); await runFlushJob( - { appends: [{ org: "o", namespace: "n" }] }, + { protectionRoots: [root], appends: [{ org: "o", namespace: "n" }] }, { post: async () => ({ stored: false, reason: "logging_disabled" }), cursor: memCursor(), @@ -82,6 +100,7 @@ test("the replies reach the recording note", async () => { test("a failed optimistic post rolls its cursor back unless a later job moved it", async () => { const job = { + protectionRoots: [establishFixtureSession(os.homedir(), "optimistic-job")], posts: [{ op: "session.brief_save", body: { a: 1 } }], cursorPath: "/b", advanceTo: 50, @@ -101,7 +120,11 @@ test("a failed optimistic post rolls its cursor back unless a later job moved it test("malformed jobs deliver nothing", async () => { const calls = []; const post = async (op) => (calls.push(op), {}); - await runFlushJob({ posts: [null, { op: 1, body: {} }, { op: "x" }], appends: "no" }, { post, cursor: memCursor() }); + const root = establishFixtureSession(os.homedir(), "malformed-job"); + await runFlushJob( + { protectionRoots: [root], posts: [null, { op: 1, body: {} }, { op: "x" }], appends: "no" }, + { post, cursor: memCursor() }, + ); await runFlushJob(null, { post, cursor: memCursor() }); assert.deepEqual(calls, []); }); diff --git a/integrations/claude-code-plugin/tests/hook-contract.test.mjs b/integrations/claude-code-plugin/tests/hook-contract.test.mjs index 7150c7da1b..cecfee943b 100644 --- a/integrations/claude-code-plugin/tests/hook-contract.test.mjs +++ b/integrations/claude-code-plugin/tests/hook-contract.test.mjs @@ -4,6 +4,7 @@ // output shape its event allows. Each case runs the real script as the host // does (`node