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);
}
});
12 changes: 12 additions & 0 deletions integrations/claude-code-plugin/tests/hook-sandbox.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from "node:fs";
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) throw new Error("unfinished detached jobs in hook sandbox");
await delay(10);
}
fs.rmSync(box.dir, { recursive: true, force: true });
}
182 changes: 182 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,182 @@
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 }), /unfinished detached jobs/);
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 });
}
}
});
8 changes: 6 additions & 2 deletions tests/integration/test_scim_person_live_ch.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,17 @@ async def people(monkeypatch: pytest.MonkeyPatch) -> AsyncIterator[People]:
query_all, query_one = client.query_all, client.query_one

async def all_rows(sql: str, params: dict[str, Any] | None = None, **kwargs: Any) -> Any:
observing_person = ch.current_query_tag() == "auth.scim_people"
answer = await query_all(sql, params=params, **kwargs)
made.results.append((sql, ch.statement_settings(sql), answer))
if observing_person:
made.results.append((sql, ch.statement_settings(sql), answer))
return answer

async def one_row(sql: str, params: dict[str, Any] | None = None, **kwargs: Any) -> Any:
observing_person = ch.current_query_tag() == "auth.scim_people"
answer = await query_one(sql, params=params, **kwargs)
made.results.append((sql, ch.statement_settings(sql), answer))
if observing_person:
made.results.append((sql, ch.statement_settings(sql), answer))
return answer

monkeypatch.setattr(client, "query_all", all_rows)
Expand Down
32 changes: 30 additions & 2 deletions tests/unit/code_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ def _linear_failure_context() -> dict[str, Any]:
}


def linear_sample_context() -> dict[str, Any]:
"""State outside a cost call; clock deltas include the observer's own work."""
try:
return {
"process_cpu_ns": time.process_time_ns(),
"monotonic_ns": time.monotonic_ns(),
"thread_cpu_ns": time.thread_time_ns(),
"allocated_blocks": sys.getallocatedblocks(),
"gc_enabled": gc.isenabled(),
"gc_count": gc.get_count(),
"trace_active": sys.gettrace() is not None,
"profile_active": sys.getprofile() is not None,
}
except Exception as error: # noqa: BLE001 — observation must not change the cost judge
return {"context_error": type(error).__name__}


def assert_linear(
cost: Callable[[Any], float],
small: Any,
Expand All @@ -66,17 +83,28 @@ def assert_linear(
quadratic one takes 16) or under `floor` seconds. On a loaded host the best of three
runs of each counts. No absolute bound: a linear cost at `big` takes several times
as long on a slow runner as on a fast one."""
samples: list[dict[str, Any]] = []
original = cost

def observed(size: Any) -> float:
before = linear_sample_context()
value = original(size)
samples.append({"cost_s": value, "before": before, "after": linear_sample_context()})
return value

cost = observed
a, b = cost(small), cost(big)
if b > ratio * a:
a, b = min(a, cost(small), cost(small)), min(b, cost(big), cost(big))
try:
assert b < ratio * a or b < floor, (a, b)
except AssertionError as error:
try:
# Diagnostic imports and allocations must follow the failed judge.
# JSON encoding and cumulative context follow the failed judge.
import json

note = "linear_cost_context=" + json.dumps(_linear_failure_context(), sort_keys=True)
context = {**_linear_failure_context(), "samples": samples}
note = "linear_cost_context=" + json.dumps(context, sort_keys=True)
except Exception as context_error: # Preserve the failed judge.
error.add_note("linear_cost_context_error=" + type(context_error).__name__)
raise error from context_error
Expand Down
25 changes: 22 additions & 3 deletions tests/unit/lane_walks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import time
from collections.abc import Callable

from tests.unit.code_fixtures import untraced
from tests.unit.code_fixtures import linear_sample_context, untraced


def _markdown(text: str) -> object:
Expand Down Expand Up @@ -506,17 +506,32 @@ def _inline_templates(n: int) -> str:
_WALK_N = 100_000


def walk_cost(name: str) -> tuple[float, float]:
def walk_cost(
name: str, *, diagnostics: list[dict[str, object]] | None = None
) -> tuple[float, float]:
"""The best of three times of the walk at N and 4N characters."""
walk, text = next((w[1], w[2]) for w in WALKS if w[0] == name)

def cost(n: int) -> float:
def timed_cost(n: int) -> float:
t = text(n)
with untraced(): # a full collection costs in proportion to the heap, not to the text
t0 = time.process_time() # CPU time: a loaded host stretches wall time, not this
walk(t)
return time.process_time() - t0

def cost(n: int) -> float:
if diagnostics is None:
return timed_cost(n)
before = linear_sample_context()
cpu_s = None
try:
cpu_s = timed_cost(n)
return cpu_s
finally:
diagnostics.append(
{"n": n, "cpu_s": cpu_s, "before": before, "after": linear_sample_context()}
)

return min(cost(_WALK_N) for _ in range(3)), min(cost(4 * _WALK_N) for _ in range(3))


Expand Down Expand Up @@ -779,6 +794,8 @@ def cost(
source = (head + text(n)).encode()
parse_s, parse_bytes, parse_raised = 0.0, 0, False
errors = {}
# These boundaries include untraced setup and restoration, outside the timed walk.
before = linear_sample_context() if samples is not None else None
with untraced():
t0 = time.process_time() # CPU time, as in `walk_cost`
if samples is None:
Expand All @@ -794,6 +811,8 @@ def cost(
"n": n,
"source_bytes": len(source),
"cpu_s": took,
"before": before,
"after": linear_sample_context(),
"parsed_bytes": parse_bytes,
"lane": lane,
"finished": finished,
Expand Down
Loading
Loading