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
37 changes: 37 additions & 0 deletions src/mount/local-mount-preflight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,43 @@ describe('ensureLocalMount', () => {
})
})

it('confirms a CLI mount that records its pid under daemon.pid (not top-level pid)', async () => {
await withTempDir(async (dir) => {
const cli = join(dir, 'relayfile')
await writeFile(cli, '#!/bin/sh\n', 'utf8')
await chmod(cli, 0o755)
resolveCliMock.mockReturnValue(cli)
const stateDir = join(dir, '.integrations', '.relay')
const statePath = join(stateDir, 'state.json')

spawnMock.mockImplementation((_cmd: string, args: string[]) => {
const child = new EventEmitter() as EventEmitter & { stderr: EventEmitter }
child.stderr = new EventEmitter()
const isStart = args[0] === 'start'
setTimeout(() => {
const finish = async (): Promise<void> => {
if (isStart) {
await mkdir(stateDir, { recursive: true })
// CLI-daemonized mount: pid lives under daemon.pid, no top-level pid.
await writeFile(statePath, JSON.stringify({
workspaceId: 'rw_test',
lastReconcileAt: new Date().toISOString(),
daemon: { pid: process.pid },
}), 'utf8')
}
}
void finish().then(() => child.emit('close', 0), () => child.emit('close', 1))
}, 0)
return child
})

await expect(ensureLocalMount('rw_test', dir, {
stateWaitTimeoutMs: 100,
stateWaitPollMs: 1,
})).resolves.toBeUndefined()
})
})

it('rejects a malformed state file instead of silently continuing', async () => {
await withTempDir(async (dir) => {
await installFakeBinary(dir)
Expand Down
12 changes: 7 additions & 5 deletions src/mount/local-mount-preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { spawn } from 'node:child_process'

import { checkMountStaleness, resolveRelayfileCli, resolveRelayfileMountBinary } from './relayfile-binary'
import { checkMountStaleness, coercePid, resolveRelayfileCli, resolveRelayfileMountBinary } from './relayfile-binary'

const STATE_FILE = '.integrations/.relay/state.json'

Expand Down Expand Up @@ -175,14 +175,16 @@ function isValidMountState(
acceptableWorkspaceIds: readonly string[] = [],
): boolean {
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false
const state = value as { workspaceId?: unknown; lastReconcileAt?: unknown; pid?: unknown }
const state = value as { workspaceId?: unknown; lastReconcileAt?: unknown; pid?: unknown; daemon?: { pid?: unknown } }
const accepted = new Set([workspaceId, ...acceptableWorkspaceIds])
// A CLI-daemonized mount (`relayfile start --background`) records its pid under
// `daemon.pid`, not the top-level `pid` — accept either, matching
// checkMountStaleness (else a freshly-started CLI mount is never confirmed ready).
const pid = coercePid(state.pid) ?? coercePid(state.daemon?.pid)
return typeof state.workspaceId === 'string' && accepted.has(state.workspaceId) &&
typeof state.lastReconcileAt === 'string' &&
Number.isFinite(Date.parse(state.lastReconcileAt)) &&
typeof state.pid === 'number' &&
Number.isInteger(state.pid) &&
state.pid > 0
pid !== undefined
}

const isAuthError = (stderr: string): boolean =>
Expand Down
2 changes: 1 addition & 1 deletion src/mount/relayfile-binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type MountState = {
daemon?: { pid?: unknown }
}

function coercePid(value: unknown): number | undefined {
export function coercePid(value: unknown): number | undefined {
return typeof value === 'number' && Number.isInteger(value) && value > 0 ? value : undefined
}

Expand Down