Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/core/test/chat/fake-claude-orphan-reap.it.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {spawn} from 'node:child_process'
import {createRequire} from 'node:module'
import {once} from 'node:events'
import {fileURLToPath, pathToFileURL} from 'node:url'
import {describe, expect, it, vi} from 'vitest'

const require = createRequire(import.meta.url)
const tsxEntry = fileURLToPath(pathToFileURL(require.resolve('tsx')))
const parentSimPath = fileURLToPath(new URL('../fixtures/orphan-parent-sim.ts', import.meta.url))

function isAlive(pid: number): boolean {
try {
process.kill(pid, 0)
return true
} catch {
return false
}
}

describe('fake-claude orphan self-reap (IT, real process kill)', () => {
it('exits on its own once its spawning parent is SIGKILLed, without anyone signaling it directly', async () => {
const parent = spawn(process.execPath, ['--import', tsxEntry, parentSimPath], {stdio: ['ignore', 'pipe', 'pipe']})
const parentPid = parent.pid
if (parentPid === undefined) throw new Error('orphan-parent-sim did not spawn')

let fixturePid: number | undefined
try {
const [chunk] = await once(parent.stdout, 'data')
if (!(chunk instanceof Buffer)) throw new Error('expected a Buffer chunk from stdout')
const match = /READY (\d+)/.exec(chunk.toString())
if (!match) throw new Error('orphan-parent-sim did not report READY')
const confirmedFixturePid = Number(match[1])
fixturePid = confirmedFixturePid
expect(isAlive(confirmedFixturePid)).toBe(true)

process.kill(parentPid, 'SIGKILL')
await vi.waitFor(() => expect(isAlive(parentPid)).toBe(false), {timeout: 2000, interval: 50})
await vi.waitFor(() => expect(isAlive(confirmedFixturePid)).toBe(false), {timeout: 5000, interval: 50})
} finally {
if (isAlive(parentPid)) process.kill(parentPid, 'SIGKILL')
if (fixturePid !== undefined && isAlive(fixturePid)) process.kill(fixturePid, 'SIGKILL')
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}, 12_000)
})
5 changes: 5 additions & 0 deletions packages/core/test/fixtures/fake-claude.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {existsSync, writeFileSync} from 'node:fs'

const spawnerPid = process.ppid
setInterval(() => {
if (process.ppid !== spawnerPid) process.exit(0)
}, 1000).unref()

const argv = process.argv.slice(2)
const argvFile = process.env.CONCIV_TEST_ARGV_FILE
if (argvFile) writeFileSync(argvFile, JSON.stringify(argv))
Expand Down
23 changes: 23 additions & 0 deletions packages/core/test/fixtures/orphan-parent-sim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {spawn} from 'node:child_process'
import {createRequire} from 'node:module'
import {existsSync, mkdtempSync} from 'node:fs'
import {tmpdir} from 'node:os'
import {join} from 'node:path'
import {fileURLToPath, pathToFileURL} from 'node:url'

const require = createRequire(import.meta.url)
const tsxEntry = fileURLToPath(pathToFileURL(require.resolve('tsx')))
const fakeClaudePath = fileURLToPath(new URL('./fake-claude.ts', import.meta.url))
const readyFile = join(mkdtempSync(join(tmpdir(), 'orphan-sim-')), 'argv.json')

const child = spawn(process.execPath, ['--import', tsxEntry, fakeClaudePath], {
stdio: 'ignore',
env: {...process.env, CONCIV_FAKE_HANG: '1', CONCIV_TEST_ARGV_FILE: readyFile},
})

const poll = setInterval(() => {
if (!existsSync(readyFile)) return
clearInterval(poll)
process.stdout.write(`READY ${child.pid}\n`)
setInterval(() => {}, 1000)
}, 50)
1 change: 1 addition & 0 deletions packages/harness-testkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@tanstack/ai": "catalog:",
"@tanstack/ai-mcp": "catalog:",
"hono": "4.12.27",
"p-timeout": "^7.0.1",
"zod": "^4.4.3"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions packages/harness-testkit/src/create-testkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {randomUUID} from 'node:crypto'
import {mkdtempSync, rmSync} from 'node:fs'
import {tmpdir} from 'node:os'
import {join} from 'node:path'
import pTimeout from 'p-timeout'
import {serveApp} from './serve-app.js'
import type {HarnessAdapter} from '@conciv/protocol/harness-types'
import {ChatContentPartSchema, CONCIV_SESSION_HEADER} from '@conciv/protocol/chat-types'
Expand Down Expand Up @@ -141,6 +142,12 @@ export function createTestkit(harness: HarnessAdapter, boot: BootApp): Testkit {
},
cleanup: async () => {
for (const abort of aborts) abort.abort()
const stopLiveSessions = async () => {
const sessions = (await rpc.sessions.list({includeHidden: true}).catch(() => [])) ?? []
const runningSessions = sessions.filter((meta) => meta.running)
await Promise.all(runningSessions.map((meta) => rpc.chat.stop({sessionId: meta.id}).catch(() => {})))
}
await pTimeout(stopLiveSessions(), {milliseconds: 3_000, fallback: () => undefined})
await app.dispose()
await served.close()
rmSync(stateRoot, {recursive: true, force: true, maxRetries: 10, retryDelay: 50})
Expand Down
Loading
Loading