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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions integrations/claude-code-plugin/tests/hook-contract.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { hostEnv } from "./host-env.mjs";
import { removeSandbox } from "./hook-sandbox.mjs";

const here = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(here, "..");
Expand Down Expand Up @@ -285,7 +286,7 @@ test("malformed, empty and non-object payloads: every hook exits 0 silently", as
assertContract(await runAll(cases), 8000);
} finally {
await loom.close();
fs.rmSync(box.dir, { recursive: true, force: true });
await removeSandbox(box);
}
});

Expand All @@ -305,7 +306,7 @@ test("no token anywhere: every hook exits 0 silently and sends no request", asyn
assert.deepEqual(loom.ops, [], "a hook without a token reached the server");
} finally {
await loom.close();
fs.rmSync(box.dir, { recursive: true, force: true });
await removeSandbox(box);
}
});

Expand All @@ -330,7 +331,7 @@ test("server unreachable, answering 401, or hanging: every hook stays within its
} finally {
await unauthorized.close();
await hang.close();
fs.rmSync(box.dir, { recursive: true, force: true });
await removeSandbox(box);
}
});

Expand All @@ -348,7 +349,7 @@ test("a host that never closes stdin does not hold a hook open", async () => {
assertContract(await runAll(cases), 8000);
} finally {
await loom.close();
fs.rmSync(box.dir, { recursive: true, force: true });
await removeSandbox(box);
}
});

Expand All @@ -366,7 +367,7 @@ test("a well-formed session against a healthy server: every hook exits 0 silentl
assert.ok(loom.ops.length > 0, "no hook reached a healthy server");
} finally {
await loom.close();
fs.rmSync(box.dir, { recursive: true, force: true });
await removeSandbox(box);
}
});

Expand All @@ -385,6 +386,6 @@ test("a payload over the stdin cap is dropped, not parsed: every hook exits 0 si
assertContract(await runAll(cases, 4), 15000);
} finally {
await loom.close();
fs.rmSync(box.dir, { recursive: true, force: true });
await removeSandbox(box);
}
});
22 changes: 22 additions & 0 deletions integrations/claude-code-plugin/tests/hook-sandbox.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fs from "node:fs";
import path from "node:path";
import { setTimeout as delay } from "node:timers/promises";

export async function removeSandbox(box, { timeoutMs = 45000 } = {}) {
const deadline = performance.now() + timeoutMs;
// The flusher removes its job only after its last cursor/recording write.
while (fs.readdirSync(box.tmp).some((name) => /^loom-flush-\d+-\d+\.json$/.test(name))) {
if (performance.now() >= deadline) {
const jobs = fs.readdirSync(box.tmp).filter((name) => /^loom-flush-\d+-\d+\.json$/.test(name));
const pending = jobs.slice(0, 8).map((name) => ({
name,
bytes: fs.statSync(path.join(box.tmp, name), { throwIfNoEntry: false })?.size ?? null,
}));
throw new Error(
`unfinished detached jobs in hook sandbox: ${JSON.stringify({ count: jobs.length, jobs: pending })}`,
);
}
await delay(10);
}
fs.rmSync(box.dir, { recursive: true, force: true });
}
211 changes: 211 additions & 0 deletions integrations/claude-code-plugin/tests/hook-sandbox.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
import "./temp-home.mjs";
import { test } from "node:test";
import assert from "node:assert/strict";
import { fork, spawn } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import http from "node:http";
import path from "node:path";
import { once } from "node:events";
import { setImmediate as immediate } from "node:timers/promises";
import { removeSandbox } from "./hook-sandbox.mjs";
import { hostEnv } from "./host-env.mjs";
import { dispatchFlush } from "../lib/flush-job.mjs";

function sandbox() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "loom-hook-drain-"));
const tmp = path.join(dir, "tmp");
fs.mkdirSync(tmp);
return { dir, tmp };
}

test("sandbox removal waits for its detached job's final write", async () => {
const box = sandbox();
const script = path.join(box.dir, "writer.cjs");
fs.writeFileSync(
script,
`
const fs = require("node:fs");
const path = require("node:path");
const root = process.argv[2];
const job = path.join(root, "tmp", "loom-flush-1-" + process.pid + ".json");
fs.writeFileSync(job, "{}");
process.send("ready");
process.once("message", () => {
fs.writeFileSync(path.join(root, "cursor"), "done");
fs.unlinkSync(job);
process.send("written", () => process.disconnect());
});
`,
);
const child = fork(script, [box.dir], {
env: hostEnv({}),
stdio: ["ignore", "ignore", "pipe", "ipc"],
detached: true,
});
const closed = once(child, "close");
let removing;
let released = false;
let stderr = "";
child.stderr.on("data", (part) => {
stderr += part;
});
try {
const [ready] = await once(child, "message");
assert.equal(ready, "ready");
let removed = false;
removing = removeSandbox(box).then(() => {
removed = true;
});
await immediate();
assert.equal(removed, false, "the owned writer still has a pending job");
assert.equal(fs.existsSync(box.dir), true);
const written = once(child, "message");
released = true;
child.send("release");
assert.equal((await written)[0], "written");
await removing;
assert.deepEqual(await closed, [0, null], stderr);
assert.equal(fs.existsSync(box.dir), false);
} finally {
if (!released && child.connected) child.send("release");
await closed;
if (removing) await removing;
fs.rmSync(box.dir, { recursive: true, force: true });
}
});

test("an unfinished job refuses removal at the cleanup deadline", async () => {
const box = sandbox();
const job = path.join(box.tmp, "loom-flush-2-3.json");
fs.writeFileSync(job, "{}");
try {
await assert.rejects(removeSandbox(box, { timeoutMs: 0 }), (error) => {
assert.match(error.message, /^unfinished detached jobs in hook sandbox: /);
const pending = JSON.parse(error.message.split("sandbox: ")[1]);
assert.equal(pending.count, 1);
assert.deepEqual(pending.jobs, [{ name: path.basename(job), bytes: 2 }]);
return true;
});
assert.equal(fs.existsSync(job), true);
} finally {
fs.rmSync(box.dir, { recursive: true, force: true });
}
});

test("an empty job census permits removal with other sandbox files", async () => {
const box = sandbox();
fs.writeFileSync(path.join(box.tmp, "cursor"), "42");
await removeSandbox(box);
assert.equal(fs.existsSync(box.dir), false);
});

test("sandbox removal waits for the real flusher's completion marker", async () => {
const box = sandbox();
let arrived;
const request = new Promise((resolve) => {
arrived = resolve;
});
let respond;
const server = http.createServer((req, res) => {
req.resume();
req.on("end", () => {
respond = () => {
if (!res.writableEnded) {
res.writeHead(200, { "content-type": "application/json" });
res.end("{}");
}
};
arrived();
});
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
let child;
let closed;
let removing;
const token = [
"e30",
Buffer.from(JSON.stringify({ org: "test", namespaces: ["scope"] })).toString("base64url"),
"test",
].join(".");
try {
assert.equal(
dispatchFlush(
{
appends: [{ org: "test", namespace: "scope", agent: "test", session_id: "test" }],
cursorPath: path.join(box.tmp, "cursor"),
advanceTo: 42,
},
{
tmp: box.tmp,
timeoutMs: 1000,
spawnImpl: (command, args, options) => {
child = spawn(command, args, {
...options,
env: hostEnv({
HOME: box.dir,
TMPDIR: box.tmp,
LOOM_BASE_URL: `http://127.0.0.1:${server.address().port}`,
LOOM_API_KEY: token,
LOOM_HOOK_TIMEOUT_MS: "1000",
LOOM_AUTO_SIGNUP: "off",
}),
});
closed = once(child, "close");
return child;
},
},
),
true,
);
await Promise.race([
request,
closed.then(() => {
throw new Error("flusher exited before its request");
}),
]);
let removed = false;
removing = removeSandbox(box).then(() => {
removed = true;
});
await immediate();
assert.equal(removed, false);
assert.equal(fs.existsSync(box.dir), true);
respond();
assert.deepEqual(await closed, [0, null]);
await removing;
assert.equal(fs.existsSync(box.dir), false);
} finally {
respond?.();
try {
if (closed) await closed;
if (removing) await removing;
} finally {
await new Promise((resolve) => server.close(resolve));
fs.rmSync(box.dir, { recursive: true, force: true });
}
}
});

test("unfinished job details are bounded and contain no job contents", async () => {
const box = sandbox();
const secret = "private-capture-not-in-diagnostics";
for (let i = 0; i < 10; i++) fs.writeFileSync(path.join(box.tmp, `loom-flush-3-${i}.json`), secret);
try {
await assert.rejects(removeSandbox(box, { timeoutMs: 0 }), (error) => {
assert.equal(error.message.includes(secret), false);
const pending = JSON.parse(error.message.split("sandbox: ")[1]);
assert.equal(pending.count, 10);
assert.equal(pending.jobs.length, 8);
for (const job of pending.jobs) {
assert.deepEqual(Object.keys(job), ["name", "bytes"]);
assert.match(job.name, /^loom-flush-3-\d+\.json$/);
assert.equal(job.bytes, Buffer.byteLength(secret));
}
return true;
});
assert.equal(fs.readdirSync(box.tmp).length, 10);
} finally {
fs.rmSync(box.dir, { recursive: true, force: true });
}
});
30 changes: 27 additions & 3 deletions tests/integration/test_private_database_log_boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from __future__ import annotations

import os
from contextlib import contextmanager
from contextvars import ContextVar
from uuid import uuid4

import httpx
Expand All @@ -11,6 +13,30 @@
from tests.integration._resources import ch_client_from_env, unique_org


@contextmanager
def _observe_request_capture(patch, capture):
from server.core import ch
from server.settings import settings

# Child work belongs to this request; tasks already running do not.
request = ContextVar("private_log_test_capture", default=False)
original_path = settings.ch_capture_statements
original_capture = ch.capture_statement

def capture_request(sql, _path):
path = str(capture) if request.get() else original_path
if path:
original_capture(sql, path)

patch.setattr(ch, "capture_statement", capture_request)
patch.setattr(settings, "ch_capture_statements", str(capture))
token = request.set(True)
try:
yield
finally:
request.reset(token)


async def test_native_private_query_log_is_inaccessible_to_sql_and_analyst(
control_plane_url, mint_test_token, monkeypatch, tmp_path
):
Expand All @@ -20,7 +46,6 @@ async def test_native_private_query_log_is_inaccessible_to_sql_and_analyst(
from server.core.diagnostics import private_work
from server.insights.app import sql_access
from server.insights.domain.sql_surface import Scope
from server.settings import settings

org, namespace = unique_org("private-log"), "docs"
token = mint_test_token(org=org, namespaces=[namespace], roles=["writer"])
Expand All @@ -32,8 +57,7 @@ async def test_native_private_query_log_is_inaccessible_to_sql_and_analyst(
try:
await ch.connect()
capture = tmp_path / "statements.jsonl"
with monkeypatch.context() as patch:
patch.setattr(settings, "ch_capture_statements", str(capture))
with monkeypatch.context() as patch, _observe_request_capture(patch, capture):
query_id = "docsrc-private-log-" + uuid4().hex
with private_work():
value = await ch.raw_bytes(
Expand Down
Loading
Loading