-
Notifications
You must be signed in to change notification settings - Fork 808
test(memory): add concurrent recall acceptance probe #1901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Wibias
wants to merge
11
commits into
dev
Choose a base branch
from
test/memory-recall-soak-820
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,256
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d3d4672
test(memory): add deterministic soak probe helpers
Wibias 12b2171
test(memory): isolate proxy metrics in soak child
Wibias 2ef5b1e
test(memory): add concurrent recall soak probe
Wibias e58ce9e
test(memory): cover soak probe determinism
Wibias c7883bb
test(memory): use portable numeric assertions
Wibias c14d612
test(memory): harden cross-platform soak child setup
Wibias 35279ca
test(memory): synchronize recall waves and report pressure peaks
Wibias 616cfc7
test(memory): keep repeated soak waves byte-identical
Wibias 8bcbcc9
fix(memory): harden soak child startup cleanup
Wibias f6bd753
test(memory): cover soak option and rng bounds
Wibias bc4baf3
fix(memory): bound soak probe requests
Wibias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /** | ||
| * #820 memory-recall soak probe child. | ||
| * | ||
| * The parent owns the mock provider and clients. This child owns only the real | ||
| * OpenCodex proxy plus a loopback-only control listener for payload-free scalar | ||
| * metrics, which keeps process RSS attributable to the proxy rather than the | ||
| * load generator. This is an offline probe, not a CI test or production route. | ||
| */ | ||
| import { mkdtempSync, rmSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import type { OcxConfig } from "../src/types"; | ||
|
|
||
| function optionValue(name: string): string | null { | ||
| const index = Bun.argv.indexOf(name); | ||
| if (index === -1 || index + 1 >= Bun.argv.length) return null; | ||
| return Bun.argv[index + 1] ?? null; | ||
| } | ||
|
|
||
| const upstreamBaseUrl = optionValue("--upstream"); | ||
| if (!upstreamBaseUrl) { | ||
| console.error("memory-recall-soak-child: --upstream is required"); | ||
| process.exit(2); | ||
| } | ||
|
|
||
| let upstreamUrl: URL; | ||
| try { | ||
| upstreamUrl = new URL(upstreamBaseUrl); | ||
| } catch { | ||
| console.error("memory-recall-soak-child: --upstream must be a URL"); | ||
| process.exit(2); | ||
| } | ||
| const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "localhost", "[::1]"]); | ||
| if (upstreamUrl.protocol !== "http:" && upstreamUrl.protocol !== "https:") { | ||
| console.error("memory-recall-soak-child: --upstream must be http or https"); | ||
| process.exit(2); | ||
| } | ||
| if (!LOOPBACK_HOSTNAMES.has(upstreamUrl.hostname)) { | ||
| console.error("memory-recall-soak-child: upstream must be loopback"); | ||
| process.exit(2); | ||
| } | ||
|
|
||
| const home = mkdtempSync(join(tmpdir(), "ocx-memory-recall-soak-")); | ||
| process.env.OPENCODEX_HOME = home; | ||
| function cleanupHome(): void { | ||
| try { rmSync(home, { recursive: true, force: true }); } catch { /* temp cleanup only */ } | ||
| } | ||
| process.on("exit", cleanupHome); | ||
|
|
||
| const [configModule, serverModule, memoryModule, lifecycleModule, relayModule, responseStateModule] = await Promise.all([ | ||
| import("../src/config"), | ||
| import("../src/server"), | ||
| import("../src/lib/app-owned-memory"), | ||
| import("../src/server/lifecycle"), | ||
| import("../src/server/relay"), | ||
| import("../src/responses/state"), | ||
| ]); | ||
|
|
||
| const config = { | ||
| port: 0, | ||
| defaultProvider: "mock", | ||
| providers: { | ||
| mock: { | ||
| adapter: "openai-chat", | ||
| baseUrl: `${upstreamUrl.toString().replace(/\/$/, "")}/v1`, | ||
| apiKey: "memory-soak-local-only", | ||
| allowPrivateNetwork: true, | ||
| }, | ||
| }, | ||
| } as OcxConfig; | ||
| configModule.saveConfig(config); | ||
| const proxy = serverModule.startServer(0); | ||
|
|
||
| function metrics() { | ||
| const usage = process.memoryUsage(); | ||
| return { | ||
| atMs: Date.now(), | ||
| pid: process.pid, | ||
| platform: process.platform, | ||
| bunVersion: Bun.version, | ||
| bunRevision: Bun.revision, | ||
| uptimeSeconds: process.uptime(), | ||
| rss: usage.rss, | ||
| heapUsed: usage.heapUsed, | ||
| heapTotal: usage.heapTotal, | ||
| external: usage.external, | ||
| arrayBuffers: usage.arrayBuffers, | ||
| activeTurnCount: lifecycleModule.getActiveTurnCount(), | ||
| appOwnedBytes: memoryModule.appOwnedBytesSnapshot(), | ||
| inspectionCounters: relayModule.getInspectionCounters(), | ||
| responseState: responseStateModule.responseStateMetrics(), | ||
| }; | ||
| } | ||
|
|
||
| let closing = false; | ||
| let control: ReturnType<typeof Bun.serve> | undefined; | ||
|
|
||
| async function closeAndExit(code: number): Promise<never> { | ||
| if (closing) { | ||
| await Bun.sleep(25); | ||
| process.exit(code); | ||
| } | ||
| closing = true; | ||
| try { await proxy.stop(true); } catch { /* best-effort probe teardown */ } | ||
| try { control?.stop(true); } catch { /* best-effort probe teardown */ } | ||
| cleanupHome(); | ||
| process.exit(code); | ||
| } | ||
|
|
||
| control = Bun.serve({ | ||
| hostname: "127.0.0.1", | ||
| port: 0, | ||
| async fetch(req) { | ||
| const url = new URL(req.url); | ||
| if (url.pathname === "/metrics" && req.method === "GET") { | ||
| return Response.json(metrics(), { | ||
| headers: { "cache-control": "no-store" }, | ||
| }); | ||
| } | ||
| if (url.pathname === "/shutdown" && req.method === "POST") { | ||
| setTimeout(() => { void closeAndExit(0); }, 0); | ||
| return Response.json({ ok: true }); | ||
| } | ||
| return new Response("not found", { status: 404 }); | ||
| }, | ||
| }); | ||
|
|
||
| console.log(JSON.stringify({ | ||
| type: "ready", | ||
| proxyUrl: proxy.url.toString(), | ||
| controlUrl: control.url.toString(), | ||
| pid: process.pid, | ||
| platform: process.platform, | ||
| bunVersion: Bun.version, | ||
| bunRevision: Bun.revision, | ||
| })); | ||
|
|
||
| for (const signal of ["SIGINT", "SIGTERM"] as const) { | ||
| process.on(signal, () => { void closeAndExit(128); }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| export interface MemoryRecallSoakOptions { | ||
| seed: number; | ||
| sustainedSessions: number; | ||
| sustainedRounds: number; | ||
| sustainedWaves: number; | ||
| burstSessions: number; | ||
| burstRounds: number; | ||
| faultSessions: number; | ||
| slowConsumerPercent: number; | ||
| cancelPercent: number; | ||
| idleDeadlineMs: number; | ||
| sampleIntervalMs: number; | ||
| } | ||
|
|
||
| export const DEFAULT_MEMORY_RECALL_SOAK_OPTIONS: MemoryRecallSoakOptions = { | ||
| seed: 820_001, | ||
| sustainedSessions: 32, | ||
| sustainedRounds: 10, | ||
| sustainedWaves: 3, | ||
| burstSessions: 64, | ||
| burstRounds: 1, | ||
| faultSessions: 32, | ||
| slowConsumerPercent: 25, | ||
| cancelPercent: 25, | ||
| idleDeadlineMs: 30_000, | ||
| sampleIntervalMs: 100, | ||
| }; | ||
|
|
||
| const QUICK_MEMORY_RECALL_SOAK_OPTIONS: MemoryRecallSoakOptions = { | ||
| ...DEFAULT_MEMORY_RECALL_SOAK_OPTIONS, | ||
| sustainedSessions: 4, | ||
| sustainedRounds: 2, | ||
| sustainedWaves: 2, | ||
| burstSessions: 8, | ||
| faultSessions: 8, | ||
| idleDeadlineMs: 10_000, | ||
| sampleIntervalMs: 50, | ||
| }; | ||
|
|
||
| const INTEGER_FLAGS: ReadonlyArray<{ | ||
| flag: string; | ||
| key: keyof MemoryRecallSoakOptions; | ||
| min: number; | ||
| max: number; | ||
| }> = [ | ||
| { flag: "--seed", key: "seed", min: 0, max: 0xffff_ffff }, | ||
| { flag: "--sessions", key: "sustainedSessions", min: 1, max: 96 }, | ||
| { flag: "--rounds", key: "sustainedRounds", min: 1, max: 100 }, | ||
| { flag: "--waves", key: "sustainedWaves", min: 2, max: 20 }, | ||
| { flag: "--burst-sessions", key: "burstSessions", min: 1, max: 96 }, | ||
| { flag: "--burst-rounds", key: "burstRounds", min: 1, max: 20 }, | ||
| { flag: "--fault-sessions", key: "faultSessions", min: 0, max: 96 }, | ||
| { flag: "--slow-percent", key: "slowConsumerPercent", min: 0, max: 100 }, | ||
| { flag: "--cancel-percent", key: "cancelPercent", min: 0, max: 100 }, | ||
| { flag: "--idle-deadline-ms", key: "idleDeadlineMs", min: 1_000, max: 120_000 }, | ||
| { flag: "--sample-interval-ms", key: "sampleIntervalMs", min: 25, max: 5_000 }, | ||
| ]; | ||
|
|
||
| export function memoryRecallSoakUsage(): string { | ||
| return [ | ||
| "Usage: bun scripts/memory-recall-soak.ts [options]", | ||
| "", | ||
| "Offline #820 acceptance/profiling probe. It starts an isolated OpenCodex child", | ||
| "against a local mock provider and emits payload-free JSON metrics.", | ||
| "", | ||
| "Options:", | ||
| " --quick Small deterministic smoke profile", | ||
| " --seed N Deterministic workload seed", | ||
| " --sessions N Sustained independent sessions (default 32)", | ||
| " --rounds N Recall rounds per sustained session (default 10)", | ||
| " --waves N Identical sustained waves (default 3)", | ||
| " --burst-sessions N Independent burst sessions (default 64)", | ||
| " --burst-rounds N Recall rounds in the burst (default 1)", | ||
| " --fault-sessions N Fault/cancel probe sessions (default 32)", | ||
| " --slow-percent N Slow-consumer share, 0..100 (default 25)", | ||
| " --cancel-percent N Fault-wave cancellation share, 0..100 (default 25)", | ||
| " --idle-deadline-ms N Cleanup invariant deadline (default 30000)", | ||
| " --sample-interval-ms N Child-memory sample cadence (default 100)", | ||
| " --help Show this help", | ||
| ].join("\n"); | ||
| } | ||
|
|
||
| export function parseMemoryRecallSoakOptions(args: readonly string[]): MemoryRecallSoakOptions { | ||
| const options = args.includes("--quick") | ||
| ? { ...QUICK_MEMORY_RECALL_SOAK_OPTIONS } | ||
| : { ...DEFAULT_MEMORY_RECALL_SOAK_OPTIONS }; | ||
| const knownFlags = new Set<string>(["--quick", "--help", ...INTEGER_FLAGS.map(row => row.flag)]); | ||
|
|
||
| for (let index = 0; index < args.length; index++) { | ||
| const arg = args[index]; | ||
| if (!arg.startsWith("--")) throw new Error(`unexpected positional argument: ${arg}`); | ||
| if (!knownFlags.has(arg)) throw new Error(`unknown option: ${arg}`); | ||
| if (arg === "--quick" || arg === "--help") continue; | ||
|
|
||
| const spec = INTEGER_FLAGS.find(row => row.flag === arg); | ||
| if (!spec) continue; | ||
| const raw = args[index + 1]; | ||
| if (raw === undefined || raw.startsWith("--")) throw new Error(`${arg} requires an integer value`); | ||
| const value = Number(raw); | ||
| if (!Number.isSafeInteger(value) || value < spec.min || value > spec.max) { | ||
| throw new Error(`${arg} must be an integer in ${spec.min}..${spec.max}`); | ||
| } | ||
| options[spec.key] = value; | ||
| index += 1; | ||
| } | ||
|
|
||
| return options; | ||
| } | ||
|
|
||
| export function mulberry32(seed: number): () => number { | ||
| let state = seed >>> 0; | ||
| return () => { | ||
| state |= 0; | ||
| state = (state + 0x6D2B79F5) | 0; | ||
| let value = Math.imul(state ^ (state >>> 15), 1 | state); | ||
| value = (value + Math.imul(value ^ (value >>> 7), 61 | value)) ^ value; | ||
| return ((value ^ (value >>> 14)) >>> 0) / 4_294_967_296; | ||
| }; | ||
| } | ||
|
|
||
| export function stableHash(text: string, seed = 0): number { | ||
| let hash = (0x811c9dc5 ^ (seed >>> 0)) >>> 0; | ||
| for (let index = 0; index < text.length; index++) { | ||
| hash ^= text.charCodeAt(index); | ||
| hash = Math.imul(hash, 0x01000193) >>> 0; | ||
| } | ||
| return hash >>> 0; | ||
| } | ||
|
|
||
| export function deterministicToolCount(sessionId: string, round: number, seed: number): number { | ||
| return 1 + (stableHash(`${sessionId}:${round}`, seed) % 8); | ||
| } | ||
|
|
||
| export function deterministicPercent(sessionId: string, salt: string, seed: number): number { | ||
| return stableHash(`${salt}:${sessionId}`, seed) % 100; | ||
| } | ||
|
|
||
| export function linearSlope(values: readonly number[]): number | null { | ||
| if (values.length < 2) return null; | ||
| const count = values.length; | ||
| const meanX = (count - 1) / 2; | ||
| const meanY = values.reduce((sum, value) => sum + value, 0) / count; | ||
| let numerator = 0; | ||
| let denominator = 0; | ||
| for (let index = 0; index < count; index++) { | ||
| const dx = index - meanX; | ||
| numerator += dx * (values[index] - meanY); | ||
| denominator += dx * dx; | ||
| } | ||
| return denominator === 0 ? null : numerator / denominator; | ||
| } | ||
|
|
||
| export function maxFinite(values: readonly number[]): number | null { | ||
| let maximum = Number.NEGATIVE_INFINITY; | ||
| for (const value of values) { | ||
| if (Number.isFinite(value) && value > maximum) maximum = value; | ||
| } | ||
| return maximum === Number.NEGATIVE_INFINITY ? null : maximum; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.