Skip to content
23 changes: 23 additions & 0 deletions scripts/native-proof/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,29 @@ and earlier messages byte-identical. For Claude it holds the system-prompt
option and the assembled MCP roster. It is not part of the default runtime
cell.

The repository carries three incompressible 2 MiB charts beside its books, so
Git objects and checkout both hold real data
([#3513](https://github.com/schlessera/brain-kit/issues/3513)). The first turn
ends with a warm read of the same source. Throughout every turn, from its
message to its settled idle status, the parent samples `GET /api/health`
every 100 ms and a WebSocket `ping`/`pong` every 200 ms. It also samples the
pool's peak physical allocation every 100 ms, from the data, metadata and
system `disk_used` in `/sys/fs/btrfs/<fsid>/allocation` rather than through the
manager, so the sampling never queues behind the app's own pool calls. It
reads the pool device's block I/O per turn. The child times each reference tool call at the model
boundary: one in-process MCP call for Claude, and for Pi the span from the
scripted answer naming the step to the request carrying its result.
`reference-integration-latency.json` records the samples' p50, p95 and maximum,
the elapsed times and the kernel, btrfs-progs, Git, Bun and candidate
identities. Every health probe must answer and every ping must return its
pong. The warm read must add no source root and be faster than the cold one.
Latencies, elapsed times and the first turn's peak are held to
`INTEGRATED_TOLERANCES` in `reference-integration.native.ts`. Those bounds
come from a hosted `runtime_host` baseline of two cells per backend, and the
worst measured value is recorded beside each bound. Pi's warm-read bound is
wider than Claude's. Pi's time includes its agent loop's round trip through
the offline provider, while Claude's is the MCP call alone.

The container has no network, `no-new-privileges` and only `SYS_ADMIN`,
`MKNOD`, `DAC_OVERRIDE`, `FOWNER`, `CHOWN`, `KILL`, `SETUID`, `SETGID` and
`SETFCAP`. It deliberately omits
Expand Down
21 changes: 16 additions & 5 deletions scripts/native-proof/reference-integration-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ const { Client } = await import("@modelcontextprotocol/sdk/client/index.js");
const { InMemoryTransport } = await import("@modelcontextprotocol/sdk/inMemory.js");
const digest = (value: unknown) => createHash("sha256").update(JSON.stringify(value)).digest("hex");

/** Every tool result and command output, keyed by step, feeds later steps and the parent's assertions. */
/**
* Every tool result and command output, keyed by step, feeds later steps and the parent's assertions.
* `elapsedMs` (#3513) is the step's time at the model boundary: for Claude, one in-process MCP tool call;
* for Pi, from the scripted answer naming the step to the next provider request carrying its result.
* Claude's commands share one worker and carry none.
*/
const state: IntegrationState = { repository, results: {} };
function record(key: string, step: IntegrationStep, index: number, value: unknown) {
function record(key: string, step: IntegrationStep, index: number, value: unknown, elapsedMs: number | null = null) {
state.results[stepKey(key, index)] = value;
event("integration_step", { backend: selectedBackend, turn: key, index, stepKind: step.kind, ...(step.kind === "tool" ? { tool: step.tool } : {}), value });
event("integration_step", { backend: selectedBackend, turn: key, index, stepKind: step.kind, ...(step.kind === "tool" ? { tool: step.tool } : {}), elapsedMs, value });
}
const turnKey = (text: string) => Object.keys(INTEGRATION_STEPS).find(key => text.includes(`Odysseus ${key}`));

Expand All @@ -38,6 +43,8 @@ const turnKey = (text: string) => Object.keys(INTEGRATION_STEPS).find(key => tex
// the provider-facing history.
const HOST_KEY = "odysseus-offline-host-key", MODEL = "claude-sonnet-4-6";
let piInference: { origin: string; close(): void } | undefined;
/** When the scripted Pi answer naming each step was sent. */
const issued = new Map<string, number>();
if (selectedBackend === "pi") {
const server = Bun.serve({ hostname: "127.0.0.1", port: 0, maxRequestBodySize: 16 * 1024 ** 2, async fetch(request) {
if (request.method !== "POST" || new URL(request.url).pathname !== "/v1/messages") return new Response("unexpected offline inference route", { status: 404 });
Expand All @@ -56,7 +63,8 @@ if (selectedBackend === "pi") {
if (state.results[stepKey(key, index)] !== undefined) continue;
const content = results.get(`toolu_${key}_${index}`);
const text = typeof content === "string" ? content : (content as { type: string; text?: string }[]).filter(part => part.type === "text").map(part => part.text).join("");
record(key, steps[index]!, index, steps[index]!.kind === "tool" ? JSON.parse(text) : text);
const sent = issued.get(stepKey(key, index));
record(key, steps[index]!, index, steps[index]!.kind === "tool" ? JSON.parse(text) : text, sent === undefined ? null : Math.round(performance.now() - sent));
}
appendFileSync(join(dir, "pi-requests.jsonl"), JSON.stringify({ turn: key, step: index, raw }) + "\n");
const step = steps[index];
Expand All @@ -68,6 +76,7 @@ if (selectedBackend === "pi") {
: [{ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }, { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: (block as { text: string }).text } }],
{ type: "content_block_stop", index: 0 }, { type: "message_delta", delta: { stop_reason: step ? "tool_use" : "end_turn", stop_sequence: null }, usage: { output_tokens: 1 } }, { type: "message_stop" }].flat();
appendFileSync(join(dir, "pi-answers.jsonl"), JSON.stringify({ turn: key, step: index, answer: { role: "assistant", content: [block] } }) + "\n");
if (step) issued.set(stepKey(key, index), performance.now());
return new Response(frames.map(frame => `event: ${(frame as { type: string }).type}\ndata: ${JSON.stringify(frame)}\n\n`).join(""), { headers: { "content-type": "text/event-stream" } });
} });
// A fictional host-held key, sent only to this offline loopback fixture.
Expand Down Expand Up @@ -112,9 +121,11 @@ const backend = selectedBackend === "pi"
names: listed.tools.map((tool: { name: string }) => tool.name), allowedTools: params.options.allowedTools });
for (const [index, step] of INTEGRATION_STEPS[key]!.entries()) {
if (step.kind === "bash") { commands.push(`printf '@@${index}@@\\n'; ${step.command(state)}`); continue; }
const began = performance.now();
const result = await client.callTool({ name: step.tool, arguments: step.input(state) });
const elapsedMs = Math.round(performance.now() - began);
const text = (result.content as { type: string; text: string }[]).map(part => part.text).join("");
record(key, step, index, JSON.parse(text));
record(key, step, index, JSON.parse(text), elapsedMs);
}
} finally { await client.close(); }
const child = params.options.spawnClaudeCodeProcess({ command: "/bin/sh", args: ["-c", commands.join("\n") || "true"],
Expand Down
2 changes: 2 additions & 0 deletions scripts/native-proof/reference-integration-steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ const request: IntegrationStep = { kind: "tool", tool: "reference_worktree", inp
/** Turn prompts carry `Odysseus <key>`. */
export const INTEGRATION_STEPS: Record<string, IntegrationStep[]> = {
// Acquire and inspect shared source, then request two worktrees; the second is released unused.
// A last, warm read of the same source (#3513) reuses its root inside the refresh window or validates it in place.
inspects: [
source(),
{ kind: "bash", command: state => `find ${sourcePath(state, "inspects", 0)}/odyssey -type f | wc -l; cat ${sourcePath(state, "inspects", 0)}/odyssey/book-23/line-49.md` },
request,
request,
{ kind: "bash", command: state => `find ${held(state, "inspects", 2)}/odyssey -type f | wc -l; cat ${held(state, "inspects", 2)}/odyssey/book-1/line-1.md` },
action("release", "inspects", 3),
source(),
],
// Source has moved to a newer commit; the worktree has not. Edit it and renew it.
corrects: [
Expand Down
Loading
Loading