Files
zhou.han 9e86a2c7b7 proc: a Linux zombie no longer holds a lock (#1198)
yetihan (#1198): a lock whose owner is a Linux zombie was held for
good. process.kill(pid, 0) succeeds on a zombie, so 15 packages' lock
and liveness checks counted an exited, unreaped owner as alive.

- scripts/proc.mjs: processStatus(pid) is dead on ESRCH, and on Linux
  also when /proc/<pid>/stat says Z or X; EPERM is "permission", any
  other error "unknown". processAlive / processExists keep each
  caller's old EPERM and unknown-error meaning.
- scripts/sync-proc.mjs copies it into each package (--check in the
  tests); every package's own check keeps its old semantics, now with
  zombies dead. agent-console's daemon bundle and window-warmup's dist
  are rebuilt.
- 15 packages bumped.

Review run at 28ef6622 (mote, HOME=$(mktemp -d)):
- node scripts/sync-proc.mjs --check: ok.
- bun test (whole repo): 8934 pass, 0 fail.
- bun scripts/check.mjs: every package ok.
- window-warmup's build reproduces its dist/plugin.js byte for byte.
  agent-console's bundle diff read line by line: the proc module plus
  esbuild's renumbered imports, nothing else (esbuild isn't installed
  on mote, so it wasn't rebuilt there).
- Real zombie on Linux (WSL, magpie-desktop): a bash child left
  unreaped by an exec'd sleep, /proc stat "Z": the old kill(0) check
  says alive, processStatus says dead; the running process says alive.

Not tried: each package's lock contention end to end.
2026-11-16 12:19:09 +08:00

484 lines
22 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bun
/**
* 浏览器登录与续期的 Chrome DevTools 协议(CDP)客户端。
*
* Chrome 以 --remote-debugging-pipe 启动:CDP 走本进程的 fd 3/4(NUL 分隔 JSON),
* 不监听任何 TCP 端口,机器上其他进程碰不到这个浏览器会话,插件也只会驱动
* 自己启动的 Chrome。整体移植自 gcp-ai 插件的管道客户端(MIT)。
*
* 专属 user-data 目录(profile)保存移动云网页登录态:每个账号/每次登录一个
* 独立 profile(目录存进账号 metadata),登录一次后 Cookie 长期存在,续期时
* 用 --headless=new 复开该账号自己的 profile,网页应用自己会刷新会话。
*/
import { spawn } from 'node:child_process'
import { constants } from 'node:fs'
import { access, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
import { createHash, randomBytes } from 'node:crypto'
import { homedir, tmpdir } from 'node:os'
import { join, dirname, resolve } from 'node:path'
import { processAlive } from './proc.mjs'
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
/** 用户配置根(XDG_CONFIG_HOME 优先,Windows 用 %APPDATA%,其余 ~/.config)。 */
const configRoot = () => process.env.XDG_CONFIG_HOME
|| (process.platform === 'win32' && process.env.APPDATA ? process.env.APPDATA : join(homedir(), '.config'))
/** 用户缓存根(Windows 用 %LOCALAPPDATA%,其余 ~/.cache)。 */
const cacheRoot = () => process.env.XDG_CACHE_HOME
|| (process.platform === 'win32' && process.env.LOCALAPPDATA ? process.env.LOCALAPPDATA : join(homedir(), '.cache'))
/** 登录态 profile 目录的根(可用 MCLAW_BROWSER_PROFILE 覆盖)。每个账号/每次
* 登录用根下的一个独立子目录,互不串票;老版本的共享目录 defaultProfile()
* 只作为老账号(metadata 里没存 profile)续期的兜底。 */
export const profilesRoot = () => process.env.MCLAW_BROWSER_PROFILE
? resolve(process.env.MCLAW_BROWSER_PROFILE.startsWith('~') ? join(homedir(), process.env.MCLAW_BROWSER_PROFILE.slice(2)) : process.env.MCLAW_BROWSER_PROFILE)
: join(configRoot(), 'magpie', 'mclaw-browser-profiles')
/** 老版本的共享 profile 目录(0.5.0 之前所有账号共用这一个)。 */
export const defaultProfile = () => process.env.MCLAW_BROWSER_PROFILE
? resolve(process.env.MCLAW_BROWSER_PROFILE.startsWith('~') ? join(homedir(), process.env.MCLAW_BROWSER_PROFILE.slice(2)) : process.env.MCLAW_BROWSER_PROFILE)
: join(configRoot(), 'magpie', 'mclaw-browser-profile')
/** 一次新登录的专属 profile 目录:随机子目录,绝不复用旧登录态——
* 换账号登录、票被服务端吊销后重登,都不会捡到上一次的 Cookie。 */
export const freshSigninProfile = () => join(profilesRoot(), `signin-${Date.now().toString(36)}-${randomBytes(4).toString('hex')}`)
// ---------------------------------------------------------------- profile 锁
// 同一 profile 的多个操作(登录/续期)串行化:mkdir 原子抢锁,抢不到就排队。
// 锁从抢到那一刻一直持到 release() 被调(浏览器关掉之后),而不是只在启动期。
const LOCK_TIMEOUT = 180_000
async function claimProfileLock(profilePath) {
const lockDir = join(cacheRoot(), 'magpie', 'mclaw', 'profiles',
createHash('sha256').update(profilePath).digest('hex').slice(0, 16))
await mkdir(dirname(lockDir), { recursive: true }).catch(() => {})
const deadline = Date.now() + LOCK_TIMEOUT
for (;;) {
try {
await mkdir(lockDir)
await writeFile(join(lockDir, 'pid'), String(process.pid)).catch(() => {})
break
} catch (error) {
if (error.code !== 'EEXIST') throw error
// 持锁进程已经死了(上次没来得及清理)就偷走这把锁。
const holder = await readFile(join(lockDir, 'pid'), 'utf8').then((t) => Number(t.trim())).catch(() => NaN)
if (Number.isFinite(holder) && holder !== process.pid) {
const alive = processAlive(holder)
if (!alive) {
await rm(lockDir, { recursive: true, force: true }).catch(() => {})
continue
}
}
if (Date.now() >= deadline) throw new Error('MClaw 浏览器 profile 被其他操作占用,请稍后重试')
await sleep(500)
}
}
let released = false
// 返回的 release 是 awaitable:调用方必须等它跑完再退出进程,否则 rm 会丢。
return async () => {
if (released) return
released = true
await rm(lockDir, { recursive: true, force: true }).catch(() => {})
}
}
// ---------------------------------------------------------------- CDP 管道客户端
const timeoutError = (message) => Object.assign(new Error(message), { name: 'TimeoutError' })
// Chrome 每写完一个 JSON 消息跟一个 NUL。
class Pipe {
constructor(child) {
this.child = child
this.toChrome = child.stdio[3]
this.fromChrome = child.stdio[4]
this.buffer = ''
this.nextId = 1
this.pending = new Map()
this.events = new Map()
this.closed = false
this.closedReason = 'Chrome was closed'
this.fromChrome.setEncoding('utf8')
this.fromChrome.on('data', (chunk) => this.read(chunk))
this.fromChrome.on('end', () => this.shut('Chrome closed the debugging connection'))
this.fromChrome.on('error', () => this.shut('Chrome closed the debugging connection'))
this.toChrome.on('error', () => this.shut('Chrome closed the debugging connection'))
child.once('exit', (code) => this.shut(`Chrome exited (${code ?? 'signal'})`))
}
read(chunk) {
this.buffer += chunk
for (let end = this.buffer.indexOf('\0'); end >= 0; end = this.buffer.indexOf('\0')) {
const raw = this.buffer.slice(0, end)
this.buffer = this.buffer.slice(end + 1)
let message
try { message = JSON.parse(raw) } catch { continue }
if (message.id === undefined) {
for (const listener of this.events.get(`${message.sessionId ?? ''}:${message.method}`) ?? []) listener(message.params ?? {})
continue
}
const pending = this.pending.get(message.id)
if (!pending) continue
this.pending.delete(message.id)
if (message.error) pending.reject(new Error(message.error.message ?? 'Chrome refused the command'))
else pending.resolve(message.result ?? {})
}
}
shut(reason) {
if (this.closed) return
this.closed = true
this.closedReason = reason
for (const { reject } of this.pending.values()) reject(new Error(reason))
this.pending.clear()
}
send(method, params = {}, sessionId) {
if (this.closed) return Promise.reject(new Error(this.closedReason))
const id = this.nextId++
const message = { id, method, params, ...(sessionId ? { sessionId } : {}) }
return new Promise((resolve, reject) => {
this.pending.set(id, { resolve, reject })
this.toChrome.write(JSON.stringify(message) + '\0')
})
}
}
// 一个标签页:导航、执行表达式、等表达式为真。
class Page {
constructor(connection, sessionId, targetId) {
this.connection = connection
this.sessionId = sessionId
this.targetId = targetId
this.closed = false
}
send(method, params) {
return this.connection.send(method, params, this.sessionId)
}
isClosed() {
return this.closed || this.connection.closed
}
async evaluate(expression) {
const { result, exceptionDetails } = await this.send('Runtime.evaluate', {
expression, returnByValue: true, awaitPromise: true,
})
if (exceptionDetails) {
throw new Error(exceptionDetails.exception?.description?.split('\n')[0] ?? exceptionDetails.text ?? '页面执行出错')
}
return result?.value
}
async goto(url, { timeout = 60000 } = {}) {
if (this.closed) throw new Error('页面已关闭,无法导航')
const { errorText } = await this.send('Page.navigate', { url })
if (errorText) throw new Error(`无法打开 ${url}:${errorText}`)
const deadline = Date.now() + Math.min(timeout, 30000)
while (Date.now() < deadline) {
try {
const state = await this.evaluate('document.readyState')
if (state && state !== 'loading') return
} catch (error) {
if (this.isClosed()) return
if (!/context|navigated|not available|Target closed/i.test(error.message)) throw error
}
await sleep(100)
}
}
async close() {
if (this.closed) return
this.closed = true
if (this.connection.closed) return
await this.connection.send('Target.closeTarget', { targetId: this.targetId }).catch(() => {})
}
}
export class Browser {
constructor(child, connection) {
this.child = child
this.connection = connection
this.pages = new Set()
}
async newPage(url = 'about:blank') {
const { targetId } = await this.connection.send('Target.createTarget', { url })
const { sessionId } = await this.connection.send('Target.attachToTarget', { targetId, flatten: true })
const page = new Page(this.connection, sessionId, targetId)
this.pages.add(page)
page.send('Page.enable').catch(() => {})
return page
}
async firstPage(timeout = 15000) {
const deadline = Date.now() + timeout
while (Date.now() < deadline) {
const { targetInfos = [] } = await this.connection.send('Target.getTargets').catch(() => ({}))
const found = targetInfos.find((target) => target.type === 'page')
if (found) {
const { sessionId } = await this.connection.send('Target.attachToTarget', { targetId: found.targetId, flatten: true })
const page = new Page(this.connection, sessionId, found.targetId)
this.pages.add(page)
page.send('Page.enable').catch(() => {})
return page
}
if (this.connection.closed) throw new Error(this.connection.closedReason)
await sleep(100)
}
throw new Error('Chrome 没有打开页面')
}
// 关进程 = 收掉 profile 会话:Cookie 留在磁盘上,也不会留下无主 Chrome。
// Promise 在进程退出后才 settle,持锁方因此能确定下一个 Chrome 不会叠上来。
async close() {
for (const page of this.pages) page.closed = true
const exited = new Promise((resolve) => {
if (this.child.exitCode !== null || this.child.signalCode !== null) resolve()
else this.child.once('exit', resolve)
})
await this.connection.send('Browser.close').catch(() => {})
this.connection.shut('Chrome was closed')
const gone = await Promise.race([
exited.then(() => true),
sleep(5000).then(() => false),
])
if (!gone) {
this.child.kill('SIGTERM')
const stopped = await Promise.race([exited.then(() => true), sleep(3000).then(() => false)])
if (!stopped) {
this.child.kill('SIGKILL')
await Promise.race([exited, sleep(2000)])
}
}
}
}
function launch(executable, args, { timeout = 30000 } = {}) {
const child = spawn(executable, [...args, '--remote-debugging-pipe'], {
stdio: ['ignore', 'ignore', 'ignore', 'pipe', 'pipe'],
detached: false,
})
const connection = new Pipe(child)
const failed = new Promise((_, reject) => {
child.once('error', (error) => {
connection.shut('Chrome could not be started')
// 常规「没装浏览器」已被 requireBrowser 的预检挡在前面;这里的 ENOENT
// 只剩竞态 / 坏软链等罕见情形,文案仍复用同一份,避免多处漂移。
reject(error.code === 'ENOENT'
? new Error(noBrowserMessage)
: new Error(`Chrome 无法启动:${error.message}`))
})
child.once('exit', (code) => {
connection.shut(`Chrome exited (${code ?? 'signal'})`)
})
})
failed.catch(() => {})
const browser = new Browser(child, connection)
// profile 已被别的 Chrome 占用时,第二次启动会立刻退出(表现为管道关闭)。
return Promise.race([browser.firstPage(timeout).then((page) => ({ browser, page })), failed])
}
// ---------------------------------------------------------------- Chrome 定位
/** 各平台的 Chromium 内核浏览器候选(按优先级)。只驱动自己 spawn 的浏览器,
* 所以不查 PATH、不用系统默认浏览器:Edge / Brave / Chromium 与 Chrome 同一套
* --remote-debugging-pipe flags 即可工作,都列进来。platform/env 可注入,测试
* 不必依赖测试机装了什么浏览器。 */
export function browserCandidates({ platform = process.platform, env = process.env } = {}) {
const pf = env.PROGRAMFILES
const pf86 = env['PROGRAMFILES(X86)']
const local = env.LOCALAPPDATA
if (platform === 'win32') {
return [
pf && join(pf, 'Google', 'Chrome', 'Application', 'chrome.exe'),
pf86 && join(pf86, 'Google', 'Chrome', 'Application', 'chrome.exe'),
local && join(local, 'Google', 'Chrome', 'Application', 'chrome.exe'),
pf && join(pf, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
pf86 && join(pf86, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
local && join(local, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
local && join(local, 'Chromium', 'Application', 'chrome.exe'),
].filter(Boolean)
}
if (platform === 'darwin') {
const home = join(homedir(), 'Applications')
return [
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
join(home, 'Google Chrome.app', 'Contents', 'MacOS', 'Google Chrome'),
'/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
'/Applications/Chromium.app/Contents/MacOS/Chromium',
join(home, 'Chromium.app', 'Contents', 'MacOS', 'Chromium'),
'/Applications/Brave Browser.app/Contents/MacOS/Brave Browser',
]
}
return [
'/usr/bin/google-chrome', '/usr/bin/google-chrome-stable',
'/usr/bin/chromium', '/usr/bin/chromium-browser',
'/usr/bin/microsoft-edge', '/snap/bin/chromium',
'/opt/google/chrome/chrome',
]
}
const noBrowserMessage = '找不到可用的 Chrome / Edge / Chromium。MClaw 的浏览器登录需要本机安装 Chromium 内核浏览器,且 magpie 运行在有图形界面的本地会话里(远程 / SSH / 容器 / 无头环境不支持弹窗)。请安装 Google Chrome,或设置 MCLAW_CHROME_PATH=/path/to/chrome 指向可执行文件。'
/** 唯一的浏览器定位入口:authorize 预检、登录、续期都走它,文案只此一份,
* 不会在多处漂移。override 会先校验存在/可执行,写错路径给专属报错而不是
* 误导性的「找不到 Chrome」。返回可执行文件路径,找不到就抛。
* candidates 是测试缝:注入后不依赖测试机装了什么浏览器。 */
export async function requireBrowser({ platform = process.platform, env = process.env, candidates } = {}) {
const override = env.MCLAW_CHROME_PATH
// Windows 上 X_OK 等同 F_OK(可执行位无意义),别用它误判。
const mode = platform === 'win32' ? constants.F_OK : constants.X_OK
if (override) {
const path = resolve(override.startsWith('~') ? join(homedir(), override.slice(2)) : override)
try { await access(path, mode); return path } catch {}
throw new Error(`MCLAW_CHROME_PATH 指向的浏览器不存在或不可执行:${path}。请改成 Chrome / Edge / Chromium 的可执行文件,或 unset 该变量让插件自动探测。`)
}
for (const path of candidates ?? browserCandidates({ platform, env })) {
try { await access(path, mode); return path } catch {}
}
throw new Error(noBrowserMessage)
}
/** 兼容入口:openMclawBrowser(登录与 headless 续期)用它拿真实环境的浏览器。 */
export const chromeExecutable = () => requireBrowser()
/** 登录(有头窗口)的前提检查:Linux 无图形界面先明确拒绝,再查浏览器。
* 只在 authorize 里调用——续期是 headless,没有界面也能跑,不该被这条挡住。
* candidates 透传给 requireBrowser,方便测试注入。 */
export async function assertBrowserSigninPrerequisites({ platform = process.platform, env = process.env, candidates } = {}) {
if (platform === 'linux' && !env.DISPLAY && !env.WAYLAND_DISPLAY) {
throw new Error('当前 Linux 会话没有图形界面(未设置 DISPLAY / WAYLAND_DISPLAY),MClaw 的浏览器登录无法弹出窗口;请在有图形界面的本地会话里运行 magpie。')
}
return requireBrowser({ platform, env, candidates })
}
// ---------------------------------------------------------------- 打开/收割
/** 用专属 profile 开 Chrome。登录用 headless: false(用户手动操作窗口),
* 续期用 headless: true(后台跑完就关)。profilePath 缺省用老共享目录
* (只服务于 metadata 里没存 profile 的老账号续期)。返回 { browser, page,
* profilePath, release }:release() 在浏览器 close() 之后调用才放锁。 */
export async function openMclawBrowser({ headless = true, startUrl = 'about:blank', profilePath } = {}) {
profilePath ||= defaultProfile()
const release = await claimProfileLock(profilePath)
try {
await mkdir(profilePath, { recursive: true, mode: 0o700 })
const args = [
`--user-data-dir=${profilePath}`,
'--no-first-run', '--no-default-browser-check',
'--disable-background-networking', '--disable-sync', '--disable-default-apps',
'--disable-component-update', '--disable-client-side-phishing-detection',
'--disable-domain-reliability',
'--disable-features=Translate,OptimizationHints,MediaRouter,DialMediaRouteProvider',
'--metrics-recording-only', '--no-service-autorun',
// Cookie 存储用 mock keychain,不碰 macOS 钥匙串 / Linux keyring。
...(process.platform === 'darwin' ? ['--use-mock-keychain'] : process.platform === 'linux' ? ['--password-store=basic'] : []),
'--window-size=1280,900',
...(headless ? ['--headless=new', '--disable-gpu'] : []),
'--crash-dumps-dir=' + tmpdir(),
startUrl,
]
const started = await launch(await chromeExecutable(), args)
return { ...started, profilePath, release }
} catch (error) {
release()
throw error
}
}
/** 读移动云登录 Cookie(含 httpOnly;CDP 直读浏览器 Cookie 库)。
* 返回 { name, value, expiresMs }(expiresMs 为 0 表示会话 Cookie)。 */
export async function harvestYunAuth(browser) {
const { cookies = [] } = await browser.connection.send('Storage.getCookies').catch(() => ({}))
const hits = cookies.filter((c) => c?.name === 'authorization' && /(^|\.)yun\.139\.com$/i.test(String(c?.domain ?? '')))
const pick = hits.find((c) => c?.value) ?? hits[0]
if (!pick?.value) return undefined
return {
name: pick.name,
value: pick.value,
expiresMs: Number.isFinite(pick.expires) && pick.expires > 0 ? pick.expires * 1000 : 0,
}
}
/** 浏览器登录全流程:有头 Chrome(新登录用全新 profile,绝不带旧票)→ 用户
* 在窗口里完成移动云登录 → 收割 authorization Cookie。
* 返回 { promise, abort }:promise resolve { cookie, profilePath, release,
* close }。abort() 随时可调(宿主取消):Chrome 还没开起来时先记标记,开出来
* 立即收尾;已开出则关浏览器、放锁。失败/取消/超时的路径还会删掉 profile 目录
* ——没人认领的登录态不该留在盘上(成功则归账号所有,绝不删)。 */
export function startBrowserHarvest({ deadlineMs = 600_000, profilePath = freshSigninProfile() } = {}) {
const session = { promise: undefined, abort: async () => {} }
session.promise = (async () => {
let aborted = false
let opened // { browser, release }:Chrome 开出后持有,成功时移交 callback
let resolved = false
session.abort = async () => {
if (aborted) return
aborted = true
if (opened) {
await opened.browser.close().catch(() => {})
await opened.release()
}
}
try {
const { browser, page, profilePath: used, release } = await openMclawBrowser({
headless: false, startUrl: 'https://yun.139.com/', profilePath,
})
opened = { browser, release }
if (aborted) throw new Error('登录已取消')
const deadline = Date.now() + deadlineMs
for (;;) {
if (aborted) throw new Error('登录已取消')
if (browser.connection.closed) throw new Error('浏览器被关闭,登录中断')
const cookie = await harvestYunAuth(browser).catch(() => undefined)
if (cookie?.value) {
resolved = true // 目录归账号所有(metadata.profile),abort 不许删
return { cookie, profilePath: used, release, close: () => browser.close() }
}
if (Date.now() >= deadline) throw timeoutError(`登录超时(${Math.round(deadlineMs / 60000)} 分钟内没有检测到移动云登录)`)
await sleep(1500)
}
} catch (error) {
if (opened) {
await opened.browser.close().catch(() => {})
await opened.release()
}
if (!resolved) await rm(profilePath, { recursive: true, force: true }).catch(() => {})
throw error
}
})()
return session
}
/** 续期:headless 复开账号自己的 profile(metadata.profile)首页,等网页应用
* 刷新会话后收割 Cookie。页面加载后多等 settleMs,给 tokenRefresh 留时间。 */
export async function browserRefreshHarvest({ profilePath, settleMs = 12_000, timeoutMs = 60_000 } = {}) {
const { browser, page, profilePath: used, release } = await openMclawBrowser({
headless: true, startUrl: 'https://yun.139.com/', profilePath,
})
try {
const deadline = Date.now() + timeoutMs
let cookie = await harvestYunAuth(browser).catch(() => undefined)
if (!cookie) {
await page.goto('https://yun.139.com/', { timeout: 30000 }).catch(() => {})
}
// 等 settle 窗口(页面应用启动 + 可能的 tokenRefresh),Cookie 有变化就用新的。
const first = cookie?.value
const settleDeadline = Math.min(Date.now() + settleMs, deadline)
while (Date.now() < settleDeadline) {
await sleep(2000)
const next = await harvestYunAuth(browser).catch(() => undefined)
if (next?.value && next.value !== first) { cookie = next; break }
cookie = next ?? cookie
if (browser.connection.closed) break
}
if (!cookie?.value) throw new Error('profile 里没有移动云登录态(请先完成浏览器登录)')
return { cookie, profilePath: used }
} finally {
await browser.close().catch(() => {})
await release()
}
}