/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format * @oncall memory_lab */ /** * `memlab-cli --record ` end to end: a real server child, a real script. * * npm run build-pkg && npm run test-units * * Runs against the BUILT dist/. */ import './test-isolation.mjs'; import assert from 'assert/strict'; import {spawn, spawnSync} from 'child_process'; import fs from 'fs'; import {test} from 'node:test'; import os from 'os'; import path from 'path'; import {fileURLToPath} from 'url'; const here = path.dirname(fileURLToPath(import.meta.url)); const cli = path.join(here, '..', 'bin', 'memlab-cli.js'); const server = path.join(here, '..', 'dist', 'index.js'); if (!fs.existsSync(server)) { console.error( 'check-memlab-cli-record: dist/index.js is missing; build first.', ); process.exit(1); } const tmp = () => fs.mkdtempSync(path.join(os.tmpdir(), 'cli-record-')); // No real uploader: a `meta` on the PATH would make the server child record // into, and upload, the real records directory. const PATH_WITHOUT_UPLOADER = (process.env.PATH ?? '') .split(path.delimiter) .filter(dir => dir === '' || !fs.existsSync(path.join(dir, 'meta'))) .join(path.delimiter); function runCli(args, extraEnv = {}) { const env = { ...process.env, MEMLAB_MCP_SERVER: server, PATH: PATH_WITHOUT_UPLOADER, MEMLAB_RECORDS_DIR: tmp(), ...extraEnv, }; delete env.MEMLAB_RECORD_FILE; if (!('MEMLAB_RECORD_UPLOAD_CMD' in extraEnv)) { delete env.MEMLAB_RECORD_UPLOAD_CMD; } return spawnSync(process.execPath, [cli, ...args], {env, encoding: 'utf8'}); } function roundScript() { const dir = tmp(); fs.writeFileSync( path.join(dir, 'run.json'), JSON.stringify({ run_id: 'r1', totals: {cycles: 30, ok: 30}, rungs: [0, 1, 2, 3].map(index => ({ index, path: `rung_${index}.heapsnapshot`, })), }), ); const steps = path.join(dir, 'steps.jsonl'); fs.writeFileSync( steps, [ JSON.stringify({tool: 'memlab_round_audit', args: {run_dir: dir}}), JSON.stringify({tool: 'memlab_server_status', args: {}}), ].join('\n'), ); return steps; } test('script --record writes the records and leaves output and exit status alone', () => { const steps = roundScript(); const file = path.join(tmp(), 'records.jsonl'); fs.writeFileSync(file, 'stale line from an earlier session\n'); const plain = runCli(['script', '--json', steps]); const recorded = runCli(['script', '--json', '--record', file, steps]); assert.equal(recorded.status, plain.status, recorded.stderr); const strip = out => JSON.parse(out).map(r => ({ ...r, text: r.text.replace(/\d[\d.,]*\s?(ms|s|MB|KB|GB|%)/g, ''), })); assert.deepEqual(strip(recorded.stdout), strip(plain.stdout)); const recs = fs .readFileSync(file, 'utf8') .split('\n') .filter(Boolean) .map(line => JSON.parse(line)); // memlab-cli makes calls of its own around the steps; they are recorded too. assert.deepEqual( recs.slice(-3).map(r => [r.kind, r.tool]), [ ['round_audit', 'memlab_round_audit'], ['tool_call', 'memlab_round_audit'], ['tool_call', 'memlab_server_status'], ], ); assert.ok(recs.slice(0, -3).every(r => r.kind === 'tool_call')); const [rec, auditCall] = recs.slice(-3); assert.equal(rec.data.run_id, 'r1'); assert.match(rec.round.key, /^[0-9a-f]{16}$/); assert.deepEqual(auditCall.round, rec.round); assert.equal((fs.statSync(file).mode % 0o1000).toString(8), '600'); }); test('without --record no file is written', () => { const steps = roundScript(); const dir = path.dirname(steps); const before = fs.readdirSync(dir).sort(); const res = runCli(['script', '--json', steps]); assert.equal(res.status, 0, res.stderr); assert.deepEqual(fs.readdirSync(dir).sort(), before); }); test('--record is refused where it cannot work', () => { const steps = roundScript(); const file = path.join(tmp(), 'r.jsonl'); const socket = runCli([ 'script', '--socket', path.join(tmp(), 's.sock'), '--record', file, steps, ]); assert.equal(socket.status, 1); assert.match(socket.stderr, /--record does not work with --socket/); const list = runCli(['list', '--record', file]); assert.equal(list.status, 1); assert.match(list.stderr, /--record works with call and script, not list/); for (const cmd of ['serve', 'doctor']) { const res = runCli([cmd, '--record', file]); assert.equal(res.status, 1); assert.match( res.stderr, new RegExp(`--record works with call and script, not ${cmd}`), ); assert.ok(!fs.existsSync(file)); } const missing = runCli(['script', '--record']); assert.equal(missing.status, 1); assert.match(missing.stderr, /--record needs a file/); const late = runCli(['script', steps, '--record', file]); assert.equal(late.status, 1); assert.match(late.stderr, /--record must come right after the command/); }); test('on a host with an uploader, the server records into the records directory without --record', () => { const steps = roundScript(); const records = tmp(); const run = runCli(['script', '--json', steps], { MEMLAB_RECORDS_DIR: records, MEMLAB_RECORD_UPLOAD_CMD: 'true', }); assert.equal(run.error, undefined); const files = fs.readdirSync(records).filter(f => f.endsWith('.jsonl')); assert.equal(files.length, 1, run.stderr); const kinds = fs .readFileSync(path.join(records, files[0]), 'utf8') .split('\n') .filter(Boolean) .map(line => JSON.parse(line).kind); assert.ok(kinds.includes('round_audit'), kinds.join(',')); }); /** Start `serve` on `sock`; resolves once it listens. */ async function startServe(sock, serverPath) { const env = { ...process.env, MEMLAB_MCP_SERVER: serverPath, PATH: PATH_WITHOUT_UPLOADER, MEMLAB_RECORDS_DIR: tmp(), }; delete env.MEMLAB_CLI_VERBOSE; delete env.MEMLAB_RECORD_FILE; delete env.MEMLAB_RECORD_UPLOAD_CMD; const serve = spawn(process.execPath, [cli, 'serve', '--socket', sock], { env, stdio: ['ignore', 'ignore', 'pipe'], }); await new Promise((resolve, reject) => { let err = ''; serve.stderr.on('data', d => { err += d; if (err.includes('serving')) resolve(); }); serve.on('exit', code => reject(new Error(`serve exited ${code}: ${err}`))); }); return serve; } test('a failed switch back to terse after a verbose call is retried before the next terse call', async () => { const dir = tmp(); // Answers `probe` with its current mode; refuses the second switch to terse. const fake = path.join(dir, 'fake-server.mjs'); fs.writeFileSync( fake, [ "import readline from 'readline';", 'let terse = false;', 'let switchesOn = 0;', 'const send = m => process.stdout.write(JSON.stringify({jsonrpc: "2.0", ...m}) + "\\n");', 'readline.createInterface({input: process.stdin}).on("line", line => {', ' const {id, method, params} = JSON.parse(line);', ' if (id == null) return;', ' if (method === "initialize") return send({id, result: {protocolVersion: "2024-11-05", capabilities: {}, serverInfo: {name: "fake", version: "1"}}});', ' if (method === "tools/list") return send({id, result: {tools: [{name: "probe", annotations: {readOnlyHint: true}}]}});', ' const {name, arguments: args} = params;', ' if (name === "memlab_snapshots") {', ' if (args.terse && ++switchesOn === 2) return send({id, error: {code: -1, message: "busy"}});', ' terse = args.terse;', ' return send({id, result: {content: [{type: "text", text: "ok"}]}});', ' }', ' send({id, result: {content: [{type: "text", text: terse ? "MODE terse" : "MODE verbose"}]}});', '});', ].join('\n'), ); const steps = path.join(dir, 'steps.jsonl'); fs.writeFileSync(steps, JSON.stringify({tool: 'probe', args: {}})); const sock = path.join(tmp(), 's.sock'); const serve = await startServe(sock, fake); try { const run = verbose => runCli(['script', '--socket', sock, steps], { MEMLAB_CLI_VERBOSE: verbose ? '1' : '', }).stdout; assert.match(run(false), /MODE terse/); assert.match(run(true), /MODE verbose/); assert.match(run(false), /MODE terse/); } finally { runCli(['stop', '--socket', sock]); serve.kill(); } }); test('a client with MEMLAB_CLI_VERBOSE=1 gets the full text from a terse `serve`', async () => { const steps = roundScript(); const sock = path.join(tmp(), 's.sock'); const serve = await startServe(sock, server); try { const run = verbose => runCli(['script', '--socket', sock, steps], { MEMLAB_CLI_VERBOSE: verbose ? '1' : '', }); const terse = run(false); assert.equal(terse.status, 0, terse.stderr); assert.match(terse.stdout, /^\|\|Check\|Finding\|$/m); const full = run(true); assert.equal(full.status, 0, full.stderr); assert.match(full.stdout, /^\| +\| Check +\| Finding +\|$/m); assert.match(run(false).stdout, /^\|\|Check\|Finding\|$/m); } finally { runCli(['stop', '--socket', sock]); serve.kill(); } });