|
| 1 | +import { AppError } from '@agent-device/kernel/errors'; |
| 2 | +import type { PerfKind } from './facades/observability.ts'; |
| 3 | +import { |
| 4 | + perfFramesUse, |
| 5 | + perfMemorySampleUse, |
| 6 | + perfMemorySnapshotUse, |
| 7 | + perfNativeCaptureRecoveryUse, |
| 8 | + perfNativeCaptureStartUse, |
| 9 | + perfProfileReportUse, |
| 10 | +} from './platform-runtime-operations.ts'; |
| 11 | + |
| 12 | +export type PerfRuntimeRequest = |
| 13 | + | Readonly<{ area: 'frames'; action: 'sample' }> |
| 14 | + | Readonly<{ |
| 15 | + area: 'memory'; |
| 16 | + action: 'sample' | 'snapshot'; |
| 17 | + kind?: PerfKind; |
| 18 | + outPath?: string; |
| 19 | + }> |
| 20 | + | Readonly<{ |
| 21 | + area: 'cpu'; |
| 22 | + subject: 'profile'; |
| 23 | + action: 'start' | 'stop' | 'report'; |
| 24 | + kind: 'xctrace' | 'simpleperf'; |
| 25 | + template?: string; |
| 26 | + outPath?: string; |
| 27 | + tracePath?: string; |
| 28 | + }> |
| 29 | + | Readonly<{ |
| 30 | + area: 'trace'; |
| 31 | + action: 'start' | 'stop'; |
| 32 | + kind: 'xctrace' | 'perfetto'; |
| 33 | + template?: string; |
| 34 | + outPath?: string; |
| 35 | + }>; |
| 36 | + |
| 37 | +export type PerfRuntimePlan = |
| 38 | + | Readonly<{ |
| 39 | + kind: 'frames'; |
| 40 | + request: Extract<PerfRuntimeRequest, { area: 'frames' }>; |
| 41 | + use: typeof perfFramesUse; |
| 42 | + }> |
| 43 | + | Readonly<{ |
| 44 | + kind: 'memory-sample'; |
| 45 | + request: Extract<PerfRuntimeRequest, { area: 'memory' }>; |
| 46 | + use: typeof perfMemorySampleUse; |
| 47 | + }> |
| 48 | + | Readonly<{ |
| 49 | + kind: 'memory-snapshot'; |
| 50 | + request: Extract<PerfRuntimeRequest, { area: 'memory' }>; |
| 51 | + use: typeof perfMemorySnapshotUse; |
| 52 | + }> |
| 53 | + | Readonly<{ |
| 54 | + kind: 'capture-start'; |
| 55 | + request: Extract<PerfRuntimeRequest, { area: 'cpu' | 'trace' }>; |
| 56 | + use: typeof perfNativeCaptureStartUse; |
| 57 | + }> |
| 58 | + | Readonly<{ |
| 59 | + kind: 'capture-stop'; |
| 60 | + request: Extract<PerfRuntimeRequest, { area: 'cpu' | 'trace' }>; |
| 61 | + }> |
| 62 | + | Readonly<{ |
| 63 | + kind: 'profile-report'; |
| 64 | + request: Extract<PerfRuntimeRequest, { area: 'cpu' }>; |
| 65 | + use: typeof perfProfileReportUse; |
| 66 | + }>; |
| 67 | + |
| 68 | +export function resolvePerfRuntimePlan(request: PerfRuntimeRequest): PerfRuntimePlan { |
| 69 | + if (request.area === 'frames') { |
| 70 | + return Object.freeze({ kind: 'frames', request, use: perfFramesUse }); |
| 71 | + } |
| 72 | + if (request.area === 'memory') { |
| 73 | + return request.action === 'snapshot' |
| 74 | + ? Object.freeze({ kind: 'memory-snapshot', request, use: perfMemorySnapshotUse }) |
| 75 | + : Object.freeze({ kind: 'memory-sample', request, use: perfMemorySampleUse }); |
| 76 | + } |
| 77 | + if (request.action === 'start') { |
| 78 | + return Object.freeze({ kind: 'capture-start', request, use: perfNativeCaptureStartUse }); |
| 79 | + } |
| 80 | + if (request.action === 'stop') return Object.freeze({ kind: 'capture-stop', request }); |
| 81 | + if (request.area !== 'cpu') { |
| 82 | + throw new AppError('INVALID_ARGS', 'perf trace action must be start or stop'); |
| 83 | + } |
| 84 | + return Object.freeze({ kind: 'profile-report', request, use: perfProfileReportUse }); |
| 85 | +} |
| 86 | + |
| 87 | +export { perfNativeCaptureRecoveryUse }; |
| 88 | + |
| 89 | +export function parsePerfRuntimeRequest( |
| 90 | + input: Readonly<{ |
| 91 | + positionals?: readonly string[]; |
| 92 | + kind?: string; |
| 93 | + outPath?: string; |
| 94 | + }>, |
| 95 | +): PerfRuntimeRequest { |
| 96 | + const values = input.positionals ?? []; |
| 97 | + const area = values[0]?.toLowerCase(); |
| 98 | + if (area === 'frames') { |
| 99 | + const action = values[1]?.toLowerCase(); |
| 100 | + if ((action !== undefined && action !== 'sample') || values.length > 2) { |
| 101 | + throw new AppError( |
| 102 | + 'INVALID_ARGS', |
| 103 | + 'perf action must be frames, memory sample|snapshot, cpu profile start|stop|report, or trace start|stop', |
| 104 | + ); |
| 105 | + } |
| 106 | + return Object.freeze({ area, action: 'sample' }); |
| 107 | + } |
| 108 | + if (area === 'memory') return parseMemoryRequest(values, input); |
| 109 | + if (area === 'cpu') return parseCpuRequest(values, input.outPath); |
| 110 | + if (area === 'trace') return parseTraceRequest(values, input.outPath); |
| 111 | + throw new AppError('INVALID_ARGS', 'perf requires frames, memory, cpu, or trace'); |
| 112 | +} |
| 113 | + |
| 114 | +// This is the finite CLI grammar boundary; each branch rejects one unsupported surface form. |
| 115 | +// fallow-ignore-next-line complexity |
| 116 | +function parseMemoryRequest( |
| 117 | + values: readonly string[], |
| 118 | + input: Readonly<{ kind?: string; outPath?: string }>, |
| 119 | +): PerfRuntimeRequest { |
| 120 | + const action = (values[1] ?? 'sample').toLowerCase(); |
| 121 | + if (action !== 'sample' && action !== 'snapshot') { |
| 122 | + throw new AppError('INVALID_ARGS', 'perf memory requires sample or snapshot'); |
| 123 | + } |
| 124 | + if (input.kind !== undefined && action !== 'snapshot') { |
| 125 | + throw new AppError('INVALID_ARGS', '--kind is only supported with perf memory snapshot'); |
| 126 | + } |
| 127 | + const kind = input.kind ?? values[2]; |
| 128 | + if (kind !== undefined && kind !== 'android-hprof' && kind !== 'memgraph') { |
| 129 | + throw new AppError( |
| 130 | + 'INVALID_ARGS', |
| 131 | + 'perf memory snapshot --kind must be android-hprof or memgraph', |
| 132 | + ); |
| 133 | + } |
| 134 | + return Object.freeze({ |
| 135 | + area: 'memory', |
| 136 | + action, |
| 137 | + ...(kind === undefined ? {} : { kind: kind as PerfKind }), |
| 138 | + ...(input.outPath === undefined ? {} : { outPath: input.outPath }), |
| 139 | + }); |
| 140 | +} |
| 141 | + |
| 142 | +// This is the finite CLI grammar boundary; each branch rejects one unsupported surface form. |
| 143 | +// fallow-ignore-next-line complexity |
| 144 | +function parseCpuRequest( |
| 145 | + values: readonly string[], |
| 146 | + flaggedOut: string | undefined, |
| 147 | +): PerfRuntimeRequest { |
| 148 | + if (values[1]?.toLowerCase() !== 'profile') { |
| 149 | + throw new AppError('INVALID_ARGS', 'perf cpu requires profile'); |
| 150 | + } |
| 151 | + const action = values[2]?.toLowerCase(); |
| 152 | + if (action !== 'start' && action !== 'stop' && action !== 'report') { |
| 153 | + throw new AppError('INVALID_ARGS', 'perf cpu profile action must be start, stop, or report'); |
| 154 | + } |
| 155 | + const kind = values[3]?.toLowerCase(); |
| 156 | + if (kind !== 'xctrace' && kind !== 'simpleperf') { |
| 157 | + throw new AppError('INVALID_ARGS', 'perf cpu profile requires --kind xctrace or simpleperf'); |
| 158 | + } |
| 159 | + const outPath = flaggedOut ?? values[5]; |
| 160 | + return Object.freeze({ |
| 161 | + area: 'cpu', |
| 162 | + subject: 'profile', |
| 163 | + action, |
| 164 | + kind, |
| 165 | + ...(values[4] ? { template: values[4] } : {}), |
| 166 | + ...(outPath ? { outPath } : {}), |
| 167 | + ...(values[6] ? { tracePath: values[6] } : {}), |
| 168 | + }); |
| 169 | +} |
| 170 | + |
| 171 | +function parseTraceRequest( |
| 172 | + values: readonly string[], |
| 173 | + flaggedOut: string | undefined, |
| 174 | +): PerfRuntimeRequest { |
| 175 | + const action = values[1]?.toLowerCase(); |
| 176 | + if (action !== 'start' && action !== 'stop') { |
| 177 | + throw new AppError('INVALID_ARGS', 'perf trace action must be start or stop'); |
| 178 | + } |
| 179 | + const kind = values[2]?.toLowerCase(); |
| 180 | + if (kind !== 'xctrace' && kind !== 'perfetto') { |
| 181 | + throw new AppError('INVALID_ARGS', 'perf trace requires --kind xctrace or perfetto'); |
| 182 | + } |
| 183 | + const outPath = flaggedOut ?? values[4]; |
| 184 | + return Object.freeze({ |
| 185 | + area: 'trace', |
| 186 | + action, |
| 187 | + kind, |
| 188 | + ...(values[3] ? { template: values[3] } : {}), |
| 189 | + ...(outPath ? { outPath } : {}), |
| 190 | + }); |
| 191 | +} |
0 commit comments