mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
fix(pi): offer model-accurate thinking levels in the create-session form (#1626)
* fix(web): filter Pi effort options by model thinkingLevelMap in new session form The create-session EffortField called getPiThinkingLevelOptions without the selected model's thinkingLevelMap, so the Pi effort select always showed the static off..high list: levels the model marks unsupported stayed visible and xhigh/max never appeared even for models that opt in. Pass the map through piSelectedModel (mirrors HappyComposer), and extend the stale-effort reset effect so a level unsupported by the newly selected model falls back to auto. * feat(cli): probe machine Pi models over RPC to carry thinkingLevelMap The machine-level Pi model probe parsed the `pi --list-models` text table, which only exposes provider/model/thinking-yes-no — thinkingLevelMap (and name/contextWindow) never reached the create-session form, so model-accurate thinking levels could not render there (xhigh/max are map-opt-in and were permanently hidden; see the companion web commit). Replace the table probe with a short-lived `pi --mode rpc` child (--no-session --no-extensions --no-skills --no-prompt-templates --no-tools) that issues get_available_models and reuses the session path's parsePiModels schema, so machine and session catalogs share one wire contract. The lightweight probe also measures faster than the table probe (~0.8s vs ~1.6-2.4s) and drops the whitespace-table parsing entirely. Cache/inflight dedupe/timeout structure is unchanged. * fix: address review findings on the Pi model probe and effort reset Three review findings on the RPC probe / effort-map change: - (high) Probe teardown killed only the direct child PID: with shell:true on Windows that is the shell, orphaning the interactive pi RPC process on every successful probe and on timeout; finish() also resolved before the process was confirmed gone. Use killProcessByChildProcess (taskkill /T on Windows, Unix tree kill, SIGKILL escalation) and settle only after the tree teardown completes. - (medium) An explicit get_available_models success:false response was discarded, so Pi's own error text was lost and the interactive child hung until the generic 15s timeout. parsePiModelsProbeLine now returns a three-way result (unrelated/models/error), also validating the response id, and the probe rejects immediately with Pi's error. - (medium) Switching an xhigh/max-capable model back to Default left the now-hidden effort in state and submitted it while the select visually fell back to auto. The reset effect now also covers model === 'auto' (undefined map) while still not resetting mid-resolve for a concrete model. Tests: probe-line failure/foreign-id cases; NewSession restore-to-Default reset and map-opt-in retention (the latter guards the former against a false green from the restore path). * fix(web): type the Pi model test mock as PiModelSummary The inline mock element type omitted thinkingLevelMap, so the new capability-driven tests failed typecheck (TS2353) and the required test job stopped before the unit tests ran. Use the shared PiModelSummary type so the mock cannot drift from the wire contract again. * fix(web): reconcile hidden Pi effort when model discovery fails A restored explicit Pi model never resolves when the machine catalog request fails, so piSelectedModel stays null for good. The reset effect required a resolved model, so it skipped reconciliation, while EffortField rendered with an undefined map and hid xhigh/max. Creation is only gated on the loading state, not on the error, so handleCreate could still forward the stale hidden level for Pi to reject or clamp. Treat a failed catalog as a settled selection (alongside Default and a resolved model) and reconcile against the undefined map; keep skipping the reset while a concrete model is still resolving without an error, so a restored xhigh/max survives until the map can prove it valid. Test asserts the spawn payload carries no effort after a failed catalog with a restored xhigh; verified it fails when the error branch is reverted. * fix(cli): honor a failed probe process-tree teardown killProcessByChildProcess reports survivors by resolving false, but the probe discarded that result and settled anyway. The Windows graceful path is taskkill /T without /F and escalates nothing on its own, so a probe child that refuses the signal would be reported as cleaned up and the catalog cached, letting interactive Pi processes accumulate across refreshes. Escalate to the forced teardown when the graceful one reports survivors, and reject (caching nothing, so the next call re-probes) when even that fails. Skip the check when the child has no pid: nothing can leak, and replacing a spawn ENOENT with a teardown error would only obscure the real failure. Adds probe lifecycle tests (spawn + teardown helper mocked) for the escalation path, the reject-and-do-not-cache path, the no-pid spawn-failure path, and the timeout path; verified they fail when the escalation is reverted. * fix(cli): keep extensions enabled for the Pi model probe --no-extensions silently dropped providers contributed through pi.registerProvider, which the old `pi --list-models` probe did list. Users with such an extension would have lost those models in the create-session form only. Verified with a project-local .pi/extensions provider in the same cwd: the probe with --no-extensions returned 29 models, while both the default run and the old table probe returned 30 including the extension's model (and its thinkingLevelMap). Dropping the flag restores parity; the extension provider now comes back with its map intact. Discovery that cannot contribute models stays disabled (--no-session, --no-skills, --no-prompt-templates, --no-tools). Cost: the probe now measures ~1.4-2.0s instead of ~0.65s, still at or below the old table probe (~1.6-2.4s) and fronted by the existing 60s cache. * fix(cli): probe Pi models from the home directory, not the runner cwd Under launchd/systemd the runner cwd is `/`, and starting Pi there is pathological: project discovery plus extensions that scan from the working directory walk the whole filesystem root. With extensions enabled (required so pi.registerProvider models still surface) the RPC probe took 16.8s at cwd=/, past the 15s timeout, so machine-level model discovery failed 100% of the time and the create-session form showed only 'Pi model discovery timed out'. Measured on macOS with 9 global extensions: cwd=/ extensions on 16.8s -> timeout cwd=/ extensions off 0.6s -> would lose extension providers cwd=$HOME extensions on 1.4s -> 29 models, complete The catalog is machine-scoped and does not depend on cwd, so probing from the home directory is both safe and representative. Verified from cwd=/ under the runner's exact launchd environment: 1.3-1.7s, 29 models. The old `pi --list-models` probe was immune because it never initialized a session; this regression arrived with the RPC probe and was missed because every earlier verification ran from a project directory. Tests assert the spawn cwd is homedir() and that --no-extensions stays absent; verified the cwd test fails when the option is removed. * fix(cli): keep the probe in the runner cwd, fall back to home only at a root Forcing every probe to the home directory fixed the launchd timeout but broke project-local discovery: a runner started inside a project stopped seeing that project's .pi/extensions providers, which the replaced --list-models probe did surface. Verified with a project-local provider: present from the project dir (30 models), absent from home (29). Use the runner cwd normally and fall back to home only when cwd is a filesystem root -- the launchd/systemd case where Pi startup walks the whole tree (16.8s, past PROBE_TIMEOUT_MS) and where there is no project to lose anyway. process.cwd() can also throw for a deleted directory, so that falls back to home too. Verified under the runner's launchd environment: from / -> home, 1.6s, 29 models; from a project dir -> that dir, 1.4s, 30 models including the project-local provider. Tests cover all three branches and were checked in both directions: pinning to home fails the project-cwd test, pinning to cwd fails the root test.
This commit is contained in:
@@ -1,43 +1,99 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { parsePiModelsTable } from './piModels'
|
||||
import { parsePiModelsProbeLine } from './piModels'
|
||||
|
||||
const SAMPLE_TABLE = `provider model context max-out thinking images
|
||||
openai-codex gpt-5.3-codex-spark 128K 128K yes no
|
||||
openai-codex gpt-5.6-sol 272K 128K yes yes
|
||||
opencode-go deepseek-v4-pro 1M 384K yes no
|
||||
opencode-go gpt-5.6-luna 1.1M 128K yes yes
|
||||
opencode-go qwen3.6-plus 1M 65.5K yes yes
|
||||
`
|
||||
const PROBE_RPC_ID = 'hapi-machine-models-probe'
|
||||
|
||||
describe('parsePiModelsTable', () => {
|
||||
it('parses provider/model columns and reasoning flag', () => {
|
||||
const models = parsePiModelsTable(SAMPLE_TABLE)
|
||||
expect(models).toEqual([
|
||||
{ provider: 'openai-codex', modelId: 'gpt-5.3-codex-spark', reasoning: true },
|
||||
{ provider: 'openai-codex', modelId: 'gpt-5.6-sol', reasoning: true },
|
||||
{ provider: 'opencode-go', modelId: 'deepseek-v4-pro', reasoning: true },
|
||||
{ provider: 'opencode-go', modelId: 'gpt-5.6-luna', reasoning: true },
|
||||
{ provider: 'opencode-go', modelId: 'qwen3.6-plus', reasoning: true },
|
||||
])
|
||||
function probeResponseLine(data: unknown): string {
|
||||
return JSON.stringify({
|
||||
id: PROBE_RPC_ID,
|
||||
type: 'response',
|
||||
command: 'get_available_models',
|
||||
success: true,
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
describe('parsePiModelsProbeLine', () => {
|
||||
it('parses the get_available_models response with full model records', () => {
|
||||
const result = parsePiModelsProbeLine(probeResponseLine({
|
||||
models: [
|
||||
{
|
||||
id: 'claude-opus-5',
|
||||
provider: 'anthropic',
|
||||
name: 'Claude Opus 5',
|
||||
contextWindow: 200000,
|
||||
reasoning: true,
|
||||
thinkingLevelMap: { off: 'off', minimal: null, xhigh: 'xhigh', max: 'max' },
|
||||
},
|
||||
{ id: 'gpt-4o', provider: 'openai', reasoning: false },
|
||||
],
|
||||
}))
|
||||
expect(result).toEqual({
|
||||
kind: 'models',
|
||||
models: [
|
||||
{
|
||||
provider: 'anthropic',
|
||||
modelId: 'claude-opus-5',
|
||||
name: 'Claude Opus 5',
|
||||
contextWindow: 200000,
|
||||
reasoning: true,
|
||||
thinkingLevelMap: { off: 'off', minimal: null, xhigh: 'xhigh', max: 'max' },
|
||||
},
|
||||
{ provider: 'openai', modelId: 'gpt-4o', reasoning: false },
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
it('skips the header, separators, and short lines', () => {
|
||||
const models = parsePiModelsTable(`provider model context max-out thinking images
|
||||
=== === === === === ===
|
||||
junk
|
||||
`)
|
||||
expect(models).toEqual([])
|
||||
it('ignores non-JSON lines and unrelated RPC traffic', () => {
|
||||
expect(parsePiModelsProbeLine('starting up...')).toBeNull()
|
||||
expect(parsePiModelsProbeLine('')).toBeNull()
|
||||
expect(parsePiModelsProbeLine('{not json')).toBeNull()
|
||||
expect(parsePiModelsProbeLine(JSON.stringify({ type: 'event', event: 'agent_start' }))).toBeNull()
|
||||
expect(parsePiModelsProbeLine(JSON.stringify({
|
||||
id: PROBE_RPC_ID, type: 'response', command: 'get_state', success: true, data: {},
|
||||
}))).toBeNull()
|
||||
})
|
||||
|
||||
it('treats non-reasoning models as reasoning false', () => {
|
||||
const models = parsePiModelsTable(`provider model context max-out thinking images
|
||||
anthropic claude-haiku 200K 64K no no
|
||||
`)
|
||||
expect(models).toEqual([{ provider: 'anthropic', modelId: 'claude-haiku', reasoning: false }])
|
||||
it('ignores a get_available_models response addressed to another request id', () => {
|
||||
expect(parsePiModelsProbeLine(JSON.stringify({
|
||||
id: 'someone-else',
|
||||
type: 'response',
|
||||
command: 'get_available_models',
|
||||
success: true,
|
||||
data: { models: [{ id: 'm', provider: 'p' }] },
|
||||
}))).toBeNull()
|
||||
})
|
||||
|
||||
it('dedupes identical provider/model pairs', () => {
|
||||
const models = parsePiModelsTable(SAMPLE_TABLE + 'openai-codex gpt-5.6-sol 272K 128K yes yes\n')
|
||||
expect(models.filter((m) => m.modelId === 'gpt-5.6-sol')).toHaveLength(1)
|
||||
it('surfaces an explicit RPC failure with its error text', () => {
|
||||
expect(parsePiModelsProbeLine(JSON.stringify({
|
||||
id: PROBE_RPC_ID,
|
||||
type: 'response',
|
||||
command: 'get_available_models',
|
||||
success: false,
|
||||
error: 'Unknown command',
|
||||
}))).toEqual({ kind: 'error', error: 'Unknown command' })
|
||||
})
|
||||
|
||||
it('falls back to a generic message when the failure carries no error text', () => {
|
||||
expect(parsePiModelsProbeLine(JSON.stringify({
|
||||
id: PROBE_RPC_ID,
|
||||
type: 'response',
|
||||
command: 'get_available_models',
|
||||
success: false,
|
||||
}))).toEqual({ kind: 'error', error: 'Pi rejected the model probe request' })
|
||||
})
|
||||
|
||||
it('returns an empty catalog for a malformed data payload', () => {
|
||||
expect(parsePiModelsProbeLine(probeResponseLine('not-an-object'))).toEqual({ kind: 'models', models: [] })
|
||||
expect(parsePiModelsProbeLine(probeResponseLine({}))).toEqual({ kind: 'models', models: [] })
|
||||
})
|
||||
|
||||
it('drops entries without an id but keeps the rest', () => {
|
||||
expect(parsePiModelsProbeLine(probeResponseLine({
|
||||
models: [
|
||||
{ provider: 'openai' },
|
||||
{ id: 'kept', provider: 'openai' },
|
||||
],
|
||||
}))).toEqual({ kind: 'models', models: [{ provider: 'openai', modelId: 'kept' }] })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { spawn } from 'node:child_process'
|
||||
import { homedir } from 'node:os'
|
||||
import { parse } from 'node:path'
|
||||
import type { PiModelSummary, PiModelsResponse } from '@hapi/protocol/apiTypes'
|
||||
import { parsePiModels } from '../../pi/schemas'
|
||||
import { killProcessByChildProcess } from '../../utils/process'
|
||||
import { getErrorMessage } from './rpcResponses'
|
||||
|
||||
export type ListPiModelsForMachineRequest = Record<string, never>
|
||||
@@ -17,93 +21,217 @@ const cache = new Map<string, CacheEntry>()
|
||||
const inflight = new Map<string, Promise<ListPiModelsForMachineResponse>>()
|
||||
|
||||
/**
|
||||
* Parse the `pi --list-models` table:
|
||||
* Machine-level Pi model discovery via a short-lived `pi --mode rpc` probe.
|
||||
*
|
||||
* ```
|
||||
* provider model context max-out thinking images
|
||||
* openai-codex gpt-5.6-sol 272K 128K yes yes
|
||||
* ```
|
||||
* The previous `pi --list-models` text-table probe lost every field the
|
||||
* table does not print — most importantly `thinkingLevelMap`, so the
|
||||
* create-session form could never offer model-accurate thinking levels
|
||||
* (xhigh/max are map-opt-in and were permanently hidden). The RPC probe
|
||||
* returns the same full model records as the session-scoped
|
||||
* `get_available_models` RPC and goes through the same `parsePiModels`
|
||||
* schema, so machine-level and session-level catalogs cannot drift.
|
||||
*
|
||||
* Column values never contain whitespace, so splitting on runs of whitespace
|
||||
* is stable. `context`/`max-out` are human sizes (`128K`, `1M`, `202.8K`);
|
||||
* they are retained as strings — the session-scoped `get_available_models`
|
||||
* RPC remains the authoritative source for the numeric context window.
|
||||
* Extensions must stay enabled: `pi.registerProvider` lets an extension
|
||||
* contribute whole model providers, and the old `--list-models` probe listed
|
||||
* those models. Verified with a project-local `.pi/extensions` provider:
|
||||
* `--no-extensions` returned 29 models while the default run and the old table
|
||||
* probe both returned 30 (the extension's model present). Disabling them would
|
||||
* silently hide those models from the create-session form only.
|
||||
*
|
||||
* Discovery that cannot contribute models is still disabled (`--no-session`,
|
||||
* `--no-skills`, `--no-prompt-templates`, `--no-tools`): no session file is
|
||||
* written and no skill/prompt/tool loading is paid for. The probe still starts
|
||||
* faster than the old table probe (~2.1s vs ~1.6-2.4s measured, with a 60s
|
||||
* cache in front of it).
|
||||
*/
|
||||
export function parsePiModelsTable(output: string): PiModelSummary[] {
|
||||
const models: PiModelSummary[] = []
|
||||
const seen = new Set<string>()
|
||||
const PI_PROBE_ARGS = [
|
||||
'--mode', 'rpc',
|
||||
'--no-session',
|
||||
'--no-skills',
|
||||
'--no-prompt-templates',
|
||||
'--no-tools',
|
||||
] as const
|
||||
|
||||
for (const rawLine of output.split(/\r?\n/)) {
|
||||
const line = rawLine.trim()
|
||||
if (!line || line.startsWith('provider') || line.startsWith('===')) {
|
||||
continue
|
||||
}
|
||||
const columns = line.split(/\s{2,}/)
|
||||
if (columns.length < 5) {
|
||||
continue
|
||||
}
|
||||
const [provider, modelId, , , thinking] = columns
|
||||
if (!provider || !modelId || seen.has(`${provider}/${modelId}`)) {
|
||||
continue
|
||||
}
|
||||
seen.add(`${provider}/${modelId}`)
|
||||
models.push({
|
||||
provider,
|
||||
modelId,
|
||||
reasoning: thinking === 'yes',
|
||||
})
|
||||
const PROBE_RPC_ID = 'hapi-machine-models-probe'
|
||||
|
||||
/**
|
||||
* Result of scanning one RPC stdout line for the probe response.
|
||||
*
|
||||
* - `null`: unrelated traffic (events, other responses, non-JSON noise).
|
||||
* - `models`: successful get_available_models response for our probe id.
|
||||
* - `error`: explicit failure response — surface Pi's own error text
|
||||
* immediately instead of letting the probe run into the generic timeout
|
||||
* (the RPC child is interactive and will not exit on its own).
|
||||
*/
|
||||
export type PiModelsProbeLineResult =
|
||||
| { kind: 'models'; models: PiModelSummary[] }
|
||||
| { kind: 'error'; error: string }
|
||||
|
||||
export function parsePiModelsProbeLine(line: string): PiModelsProbeLineResult | null {
|
||||
const trimmed = line.trim()
|
||||
if (!trimmed.startsWith('{')) return null
|
||||
let parsed: unknown
|
||||
try {
|
||||
parsed = JSON.parse(trimmed)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
if (typeof parsed !== 'object' || parsed === null) return null
|
||||
const record = parsed as Record<string, unknown>
|
||||
if (record.type !== 'response' || record.command !== 'get_available_models') return null
|
||||
// Only accept the response to our own request id so a future probe that
|
||||
// multiplexes RPCs cannot mis-attribute another get_available_models call.
|
||||
if (record.id !== PROBE_RPC_ID) return null
|
||||
if (record.success !== true) {
|
||||
const error = typeof record.error === 'string' && record.error.trim()
|
||||
? record.error
|
||||
: 'Pi rejected the model probe request'
|
||||
return { kind: 'error', error }
|
||||
}
|
||||
return { kind: 'models', models: parsePiModels(record.data) }
|
||||
}
|
||||
|
||||
return models
|
||||
/**
|
||||
* Working directory for the probe child.
|
||||
*
|
||||
* Normally the runner's own cwd: a runner started inside a project must keep
|
||||
* seeing that project's `.pi/extensions` providers, which the replaced
|
||||
* `--list-models` probe also surfaced (verified: a project-local provider is
|
||||
* present from the project dir and absent from home).
|
||||
*
|
||||
* A filesystem root is the exception. Under launchd/systemd the runner cwd is
|
||||
* `/`, and starting Pi there is pathological — project discovery plus
|
||||
* extensions that scan from the working directory walk the entire tree.
|
||||
* Measured on macOS with 9 global extensions: 16.8s at `/` (past
|
||||
* PROBE_TIMEOUT_MS, so discovery failed on every call) versus 1.3s at home
|
||||
* and 2.8s in a real project. There is no project to discover at a root
|
||||
* anyway, so home loses nothing there.
|
||||
*/
|
||||
export function resolveProbeCwd(): string {
|
||||
let cwd: string
|
||||
try {
|
||||
cwd = process.cwd()
|
||||
} catch {
|
||||
// cwd can be gone (deleted directory); home is always resolvable.
|
||||
return homedir()
|
||||
}
|
||||
return cwd === parse(cwd).root ? homedir() : cwd
|
||||
}
|
||||
|
||||
function runPiModelsProbe(): Promise<ListPiModelsForMachineResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn('pi', ['--list-models'], {
|
||||
const child = spawn('pi', [...PI_PROBE_ARGS], {
|
||||
env: process.env,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
// Probe from the runner's cwd, falling back to home at a
|
||||
// filesystem root (see resolveProbeCwd).
|
||||
cwd: resolveProbeCwd(),
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
shell: process.platform === 'win32',
|
||||
windowsHide: process.platform === 'win32',
|
||||
})
|
||||
let stdout = ''
|
||||
let stdoutBuffer = ''
|
||||
let stderr = ''
|
||||
let settled = false
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
const finish = (settle: () => void) => {
|
||||
if (settled) return
|
||||
settled = true
|
||||
child.kill('SIGTERM')
|
||||
reject(new Error('Pi model discovery timed out'))
|
||||
clearTimeout(timeout)
|
||||
// The probe child has no further use once the response (or a
|
||||
// failure) landed; never leave an interactive pi process behind.
|
||||
// killProcessByChildProcess tears down the whole tree (taskkill /T
|
||||
// on Windows, where shell:true makes `child` the shell; tree kill
|
||||
// on Unix) and returns false when something survived.
|
||||
//
|
||||
// A surviving process must not be ignored: the Windows graceful
|
||||
// path is `taskkill /T` without /F and escalates nothing on its
|
||||
// own, so a repeatedly-refused probe child would accumulate
|
||||
// interactive Pi processes. Escalate to the forced path, and only
|
||||
// settle once the tree is confirmed gone — the caller never
|
||||
// observes completion while a probe child is still alive.
|
||||
void (async () => {
|
||||
// No pid means the child never started (e.g. spawn ENOENT):
|
||||
// there is nothing to leak, and the real spawn error is
|
||||
// already on its way through the 'error' handler.
|
||||
if (!child.pid) {
|
||||
settle()
|
||||
return
|
||||
}
|
||||
let stopped = false
|
||||
try {
|
||||
stopped = await killProcessByChildProcess(child, false)
|
||||
if (!stopped) {
|
||||
stopped = await killProcessByChildProcess(child, true)
|
||||
}
|
||||
} catch {
|
||||
stopped = false
|
||||
}
|
||||
if (!stopped) {
|
||||
// Report the leak instead of caching a result that came
|
||||
// with an orphaned child; the next call re-probes.
|
||||
reject(new Error(
|
||||
`Pi model probe could not be stopped (pid ${child.pid ?? 'unknown'}); refusing to report a result with a surviving probe process`
|
||||
))
|
||||
return
|
||||
}
|
||||
settle()
|
||||
})()
|
||||
}
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
finish(() => reject(new Error('Pi model discovery timed out')))
|
||||
}, PROBE_TIMEOUT_MS)
|
||||
|
||||
child.stdout?.on('data', (chunk) => {
|
||||
stdout += chunk.toString()
|
||||
stdoutBuffer += chunk.toString()
|
||||
// The RPC stream is line-delimited JSON; scan every complete line
|
||||
// for the get_available_models response and ignore the rest
|
||||
// (lifecycle events, unrelated responses).
|
||||
let newlineIndex = stdoutBuffer.indexOf('\n')
|
||||
while (newlineIndex !== -1 && !settled) {
|
||||
const line = stdoutBuffer.slice(0, newlineIndex)
|
||||
stdoutBuffer = stdoutBuffer.slice(newlineIndex + 1)
|
||||
newlineIndex = stdoutBuffer.indexOf('\n')
|
||||
const probeResult = parsePiModelsProbeLine(line)
|
||||
if (probeResult === null) continue
|
||||
if (probeResult.kind === 'error') {
|
||||
// Explicit RPC failure: surface Pi's own error right away
|
||||
// instead of degrading it into the generic timeout.
|
||||
finish(() => reject(new Error(probeResult.error)))
|
||||
return
|
||||
}
|
||||
finish(() => resolve({
|
||||
success: true,
|
||||
availableModels: probeResult.models,
|
||||
currentModelId: null,
|
||||
}))
|
||||
return
|
||||
}
|
||||
})
|
||||
child.stderr?.on('data', (chunk) => {
|
||||
stderr += chunk.toString()
|
||||
})
|
||||
child.on('error', (error) => {
|
||||
if (settled) return
|
||||
settled = true
|
||||
clearTimeout(timeout)
|
||||
reject(error)
|
||||
finish(() => reject(error))
|
||||
})
|
||||
child.on('close', (code) => {
|
||||
if (settled) return
|
||||
settled = true
|
||||
clearTimeout(timeout)
|
||||
if (code !== 0) {
|
||||
reject(new Error(
|
||||
stderr.trim() || `pi --list-models exited with code ${code ?? 'unknown'}`
|
||||
))
|
||||
return
|
||||
}
|
||||
const availableModels = parsePiModelsTable(stdout)
|
||||
resolve({ success: true, availableModels, currentModelId: null })
|
||||
finish(() => reject(new Error(
|
||||
stderr.trim() || `pi exited with code ${code ?? 'unknown'} before answering the model probe`
|
||||
)))
|
||||
})
|
||||
// pi exiting before the request lands must surface as the close-path
|
||||
// error, not an unhandled EPIPE crash.
|
||||
child.stdin?.on('error', () => { /* handled via close */ })
|
||||
child.stdin?.write(`${JSON.stringify({ id: PROBE_RPC_ID, type: 'get_available_models' })}\n`)
|
||||
})
|
||||
}
|
||||
|
||||
/** Clear the module-level probe cache and in-flight map between tests. */
|
||||
export function _resetPiModelsCacheForTests(): void {
|
||||
cache.clear()
|
||||
inflight.clear()
|
||||
}
|
||||
|
||||
export async function listPiModelsForMachine(): Promise<ListPiModelsForMachineResponse> {
|
||||
const now = Date.now()
|
||||
const cached = cache.get('default')
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
import { EventEmitter } from 'node:events'
|
||||
import { homedir } from 'node:os'
|
||||
import { parse } from 'node:path'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { spawnMock, killMock } = vi.hoisted(() => ({
|
||||
spawnMock: vi.fn(),
|
||||
killMock: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('node:child_process', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('node:child_process')>()
|
||||
return { ...actual, spawn: spawnMock }
|
||||
})
|
||||
|
||||
vi.mock('../../utils/process', () => ({
|
||||
killProcessByChildProcess: killMock,
|
||||
}))
|
||||
|
||||
import { _resetPiModelsCacheForTests, listPiModelsForMachine } from './piModels'
|
||||
|
||||
const PROBE_RPC_ID = 'hapi-machine-models-probe'
|
||||
|
||||
class FakeChild extends EventEmitter {
|
||||
stdout = new EventEmitter()
|
||||
stderr = new EventEmitter()
|
||||
stdin = Object.assign(new EventEmitter(), { write: vi.fn() })
|
||||
pid: number | undefined = 4242
|
||||
kill = vi.fn()
|
||||
|
||||
emitModelsResponse(models: Array<Record<string, unknown>>): void {
|
||||
this.stdout.emit('data', `${JSON.stringify({
|
||||
id: PROBE_RPC_ID,
|
||||
type: 'response',
|
||||
command: 'get_available_models',
|
||||
success: true,
|
||||
data: { models },
|
||||
})}\n`)
|
||||
}
|
||||
}
|
||||
|
||||
let child: FakeChild
|
||||
|
||||
beforeEach(() => {
|
||||
_resetPiModelsCacheForTests()
|
||||
child = new FakeChild()
|
||||
spawnMock.mockReset()
|
||||
spawnMock.mockImplementation(() => child)
|
||||
killMock.mockReset()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
describe('runPiModelsProbe spawn', () => {
|
||||
it('probes from the runner cwd so project-local providers stay visible', async () => {
|
||||
// A runner started inside a project must keep seeing that project's
|
||||
// .pi/extensions providers, which the replaced --list-models probe
|
||||
// also surfaced.
|
||||
killMock.mockResolvedValue(true)
|
||||
const cwdSpy = vi.spyOn(process, 'cwd').mockReturnValue('/work/project')
|
||||
|
||||
const pending = listPiModelsForMachine()
|
||||
child.emitModelsResponse([{ id: 'm1', provider: 'p1' }])
|
||||
await pending
|
||||
|
||||
expect(spawnMock).toHaveBeenCalledWith(
|
||||
'pi',
|
||||
expect.any(Array),
|
||||
expect.objectContaining({ cwd: '/work/project' }),
|
||||
)
|
||||
cwdSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('falls back to home when the runner cwd is a filesystem root', async () => {
|
||||
// Under launchd/systemd the runner cwd is `/`, where a Pi startup that
|
||||
// loads extensions took 16.8s locally and blew the 15s timeout on every
|
||||
// call (1.3s from home). No project exists at a root to lose.
|
||||
killMock.mockResolvedValue(true)
|
||||
const cwdSpy = vi.spyOn(process, 'cwd').mockReturnValue(parse(process.cwd()).root)
|
||||
|
||||
const pending = listPiModelsForMachine()
|
||||
child.emitModelsResponse([{ id: 'm1', provider: 'p1' }])
|
||||
await pending
|
||||
|
||||
expect(spawnMock).toHaveBeenCalledWith(
|
||||
'pi',
|
||||
expect.any(Array),
|
||||
expect.objectContaining({ cwd: homedir() }),
|
||||
)
|
||||
cwdSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('falls back to home when the runner cwd cannot be resolved', async () => {
|
||||
// A deleted working directory makes process.cwd() throw; home always
|
||||
// resolves, so discovery must not die with it.
|
||||
killMock.mockResolvedValue(true)
|
||||
const cwdSpy = vi.spyOn(process, 'cwd').mockImplementation(() => {
|
||||
throw new Error('ENOENT: uv_cwd')
|
||||
})
|
||||
|
||||
const pending = listPiModelsForMachine()
|
||||
child.emitModelsResponse([{ id: 'm1', provider: 'p1' }])
|
||||
await pending
|
||||
|
||||
expect(spawnMock).toHaveBeenCalledWith(
|
||||
'pi',
|
||||
expect.any(Array),
|
||||
expect.objectContaining({ cwd: homedir() }),
|
||||
)
|
||||
cwdSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('keeps extensions enabled so registered providers still surface', async () => {
|
||||
// pi.registerProvider lets an extension contribute whole providers, and
|
||||
// the replaced `--list-models` probe listed those models.
|
||||
killMock.mockResolvedValue(true)
|
||||
|
||||
const pending = listPiModelsForMachine()
|
||||
child.emitModelsResponse([{ id: 'm1', provider: 'p1' }])
|
||||
await pending
|
||||
|
||||
const args = spawnMock.mock.calls[0]?.[1] as string[]
|
||||
expect(args).not.toContain('--no-extensions')
|
||||
expect(args).toEqual(expect.arrayContaining(['--mode', 'rpc', '--no-session']))
|
||||
})
|
||||
})
|
||||
|
||||
describe('runPiModelsProbe teardown', () => {
|
||||
it('escalates to a forced kill when the graceful teardown reports survivors', async () => {
|
||||
killMock.mockResolvedValueOnce(false).mockResolvedValueOnce(true)
|
||||
|
||||
const pending = listPiModelsForMachine()
|
||||
child.emitModelsResponse([{ id: 'm1', provider: 'p1' }])
|
||||
const response = await pending
|
||||
|
||||
expect(response.availableModels).toEqual([{ provider: 'p1', modelId: 'm1' }])
|
||||
expect(killMock).toHaveBeenNthCalledWith(1, child, false)
|
||||
expect(killMock).toHaveBeenNthCalledWith(2, child, true)
|
||||
})
|
||||
|
||||
it('rejects and caches nothing when even the forced teardown fails', async () => {
|
||||
killMock.mockResolvedValue(false)
|
||||
|
||||
const pending = listPiModelsForMachine()
|
||||
child.emitModelsResponse([{ id: 'm1', provider: 'p1' }])
|
||||
await expect(pending).rejects.toThrow(/could not be stopped/)
|
||||
|
||||
// A surviving probe child must not leave a cached catalog behind: the
|
||||
// next call has to re-probe rather than serve a result that came with
|
||||
// an orphan.
|
||||
killMock.mockReset()
|
||||
killMock.mockResolvedValue(true)
|
||||
const secondChild = new FakeChild()
|
||||
spawnMock.mockImplementation(() => secondChild)
|
||||
|
||||
const second = listPiModelsForMachine()
|
||||
secondChild.emitModelsResponse([{ id: 'm2', provider: 'p2' }])
|
||||
await expect(second).resolves.toMatchObject({
|
||||
availableModels: [{ provider: 'p2', modelId: 'm2' }],
|
||||
})
|
||||
expect(spawnMock).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('does not report a teardown failure when the child never started', async () => {
|
||||
child.pid = undefined
|
||||
killMock.mockResolvedValue(false)
|
||||
|
||||
const pending = listPiModelsForMachine()
|
||||
child.emit('error', new Error('spawn pi ENOENT'))
|
||||
|
||||
// The spawn failure must survive as-is instead of being replaced by a
|
||||
// misleading "could not be stopped" error.
|
||||
await expect(pending).rejects.toThrow(/ENOENT/)
|
||||
expect(killMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('tears the tree down once on the timeout path', async () => {
|
||||
vi.useFakeTimers()
|
||||
killMock.mockResolvedValue(true)
|
||||
|
||||
// Attach the rejection assertion before advancing timers so the
|
||||
// rejection can never escape as an unhandled one.
|
||||
const assertion = expect(listPiModelsForMachine()).rejects.toThrow(/timed out/)
|
||||
await vi.advanceTimersByTimeAsync(15_000)
|
||||
await assertion
|
||||
expect(killMock).toHaveBeenCalledWith(child, false)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user