Swadhin Biswas feat: implement mervelas cli engine, built-in tools, and reactive UI · 4/3/2026
0358986
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { spawn } from 'child_process'
import {
existsSync,
mkdirSync,
readdirSync,
readFileSync,
statSync,
writeFileSync,
} from 'fs'
import { join, resolve } from 'path'
type Mode = 'startup' | 'query'
type RunSummary = {
mode: Mode
command: string[]
artifactDir: string
startupReportPath: string | null
queryReportPath: string | null
wallTimeMs: number
exitCode: number
startupTotalMs: number | null
queryTtftMs: number | null
queryTotalTimeMs: number | null
queryPreApiOverheadMs: number | null
}
function fail(message: string): never {
process.stderr.write(`${message}\n`)
process.exit(1)
}
function parseArgs(argv: string[]): { mode: Mode; cliArgs: string[] } {
const [mode, ...rest] = argv
if (mode !== 'startup' && mode !== 'query') {
fail(
'Usage: bun run scripts/perf.ts <startup|query> -- <mervelas args...>',
)
}
const separatorIdx = rest.indexOf('--')
const cliArgs = separatorIdx === -1 ? rest : rest.slice(separatorIdx + 1)
return { mode, cliArgs }
}
function ensureBuilt(): void {
if (!existsSync(resolve('dist/cli.mjs'))) {
fail('Missing dist/cli.mjs. Run `bun run build` before running perf scripts.')
}
}
function getRunId(): string {
return new Date().toISOString().replace(/[:.]/g, '-')
}
function getLatestFile(dir: string): string | null {
if (!existsSync(dir)) return null
const files = readdirSync(dir)
.map(name => join(dir, name))
.filter(path => statSync(path).isFile())
.sort(
(a, b) => statSync(b).mtimeMs - statSync(a).mtimeMs,
)
return files[0] ?? null
}
function extractMs(report: string, regex: RegExp): number | null {
const match = report.match(regex)
if (!match?.[1]) return null
const value = Number.parseFloat(match[1])
return Number.isFinite(value) ? value : null
}
function readMaybe(path: string | null): string | null {
if (!path) return null
return readFileSync(path, 'utf8')
}
async function runCommand(
mode: Mode,
cliArgs: string[],
artifactDir: string,
): Promise<RunSummary> {
const configDir = join(artifactDir, 'config')
const debugDir = join(artifactDir, 'debug')
mkdirSync(configDir, { recursive: true })
mkdirSync(debugDir, { recursive: true })
const childArgs = ['dist/cli.mjs', ...cliArgs]
if (
mode === 'query' &&
!cliArgs.some(
arg => arg === '--debug-file' || arg.startsWith('--debug-file='),
)
) {
childArgs.splice(1, 0, '--debug-file', join(debugDir, 'query-debug.txt'))
}
const env = {
...process.env,
MERVELAS_CONFIG_DIR: configDir,
CLAUDE_CODE_DEBUG_LOGS_DIR: debugDir,
CLAUDE_CODE_PROFILE_STARTUP: mode === 'startup' ? '1' : '0',
CLAUDE_CODE_PROFILE_QUERY: mode === 'query' ? '1' : '0',
}
const startedAt = Date.now()
const exitCode = await new Promise<number>((resolveExit, reject) => {
const child = spawn('node', childArgs, {
cwd: resolve('.'),
env,
stdio: 'inherit',
})
child.on('error', reject)
child.on('exit', code => resolveExit(code ?? 1))
})
const wallTimeMs = Date.now() - startedAt
const startupReportPath = getLatestFile(join(configDir, 'startup-perf'))
const queryReportPath = getLatestFile(join(configDir, 'query-perf'))
const startupReport = readMaybe(startupReportPath)
const queryReport = readMaybe(queryReportPath)
return {
mode,
command: ['node', ...childArgs],
artifactDir,
startupReportPath,
queryReportPath,
wallTimeMs,
exitCode,
startupTotalMs: startupReport
? extractMs(startupReport, /Total startup time:\s+([0-9.]+)ms/)
: null,
queryTtftMs: queryReport
? extractMs(queryReport, /Total TTFT:\s+([0-9.]+)ms/)
: null,
queryTotalTimeMs: queryReport
? extractMs(queryReport, /Total time:\s+([0-9.]+)ms/)
: null,
queryPreApiOverheadMs: queryReport
? extractMs(queryReport, /Total pre-API overhead\s+([0-9.]+)ms/)
: null,
}
}
const { mode, cliArgs } = parseArgs(process.argv.slice(2))
ensureBuilt()
const artifactDir = resolve('.perf-artifacts', `${mode}-${getRunId()}`)
mkdirSync(artifactDir, { recursive: true })
const summary = await runCommand(mode, cliArgs, artifactDir)
const summaryPath = join(artifactDir, 'summary.json')
writeFileSync(summaryPath, `${JSON.stringify(summary, null, 2)}\n`, 'utf8')
process.stdout.write(`${JSON.stringify(summary, null, 2)}\n`)
process.stdout.write(`Summary written to ${summaryPath}\n`)
if (summary.exitCode !== 0) {
process.exit(summary.exitCode)
}