mirror of
https://github.com/stablyai/orca.git
synced 2026-09-29 08:03:20 +00:00
* fix(crash-reporting): make the own-Chromium gate a real choke point
Round-3 review found the guard was not the choke point its own comments
claimed: six pid-addressed `taskkill /pid <pid> /t /f` families in main were
ungated and uninstrumented, so the stale-pid shape stayed producible and a
`selfInitiatedTreeKillCount: 0` could read as exculpatory when it was not.
- Gate the remaining main-process families: the git command-runner abort, the
notebook-cell and automation-precheck timeouts.
- Turn the `src/shared` seam into the gate itself (`process-tree-kill-gate`), so
the runProcess choke point, the codex app-server deadline kill and the
ephemeral-VM recipe kill ask the same decision. Those three are compiled into
the CLI/relay too and cannot import main; main installs the guard at preflight.
- Ratchet (`main-process-tree-kill-gate.test.ts`): a new pid-addressed taskkill
in main that skips the gate fails, and the allowlist entries must still exist.
- Give pid-addressed kills eviction priority in the 32-entry ring: 32 routine
`win-pty-job` teardowns from a window-close burst no longer evict the one
entry that discriminates a self-kill from an external one.
- Correct the coverage doc, which described the uninstrumented Windows sites as
POSIX `process.kill(-pid)` group kills and omitted the git and codex paths.
* fix(crash-reporting): keep a refused tree-kill from leaking the root it owns
A refusal must block the pid-addressed tree walk, not the termination. Five of
the six gated sites returned on refusal with no fallback, so a refused
`taskkill /pid /t /f` left git.exe, a timed-out notebook cell, an automation
precheck or an ephemeral-VM recipe running while the caller reported it stopped.
The root kill is addressed by the child handle, which cannot reach the recycled
pid the refusal is about, so it stays correct and required on that path.
Also fixes the ring eviction the scope preference introduced: with the ring
saturated by pid-addressed kills, the only non-pid-addressed entry is the one
just pushed, so the splice evicted itself and the detail came back `{}` --
byte-identical to the external-kill arm, in the window-close case the guard
exists for. Eviction now excludes the newest entry and falls back to FIFO.
Tests: refusal now asserts the root kill at all six sites, and the ring covers
the saturated-pid ordering as well as round 3's group-burst ordering.
* fix(crash-reporting): stop a refused tree-kill leaking the commit-message agent, and count call sites
Two round-5 blocking findings, both open on main and on both branches.
`killSourceControlAgentProcess` had no root-kill fallback on its win32 arm: the
taskkill was the only termination, so once the own-Chromium gate could refuse it
the promise resolved having killed nothing. Both callers do
`terminationComplete ??= killSourceControlAgentProcess(child)` and then release
the managed-home lock on that promise, so a refusal left the local Codex/Claude
commit-message agent running while the caller reported it stopped -- the
lock-contention failure the taskkill was added for. Same fix as the six sibling
sites: the handle-addressed root kill cannot reach the recycled pid the refusal
is about, so it stays correct and required on that path.
The ratchet was file-granular, not call-site granular: one gate mention anywhere
in a file exempted every taskkill in it, which left the six files that now ask
the gate ratchet-blind -- the inverse of what it is for. It now counts `/pid`
call sites against gate admissions per file, so a second ungated kill inside an
existing family fails. Keying on the `/pid` argument rather than a quoted
`taskkill` also catches a kill whose program name comes from a constant. The
three comments that claimed more than the old scan enforced now state the rule
and its two remaining blind spots.
Also: the recording in `admitSelfInitiatedTreeKill` is now wrapped the way the
`admitProcessTreeKill` seam already wraps it, with the refusal decision taken
before anything that can throw so a diagnostics failure cannot flip it; and
`orca-chromium-process-pids` documents the false-positive direction (a stale
`getAppMetrics()` entry plus pid reuse refuses a live unrelated child), which is
the mechanism the root-kill fallback exists to bound.
Tests: refusal now asserts the root kill at all seven sites; the ratchet asserts
call-site counting and the constant-program form.
* test(crash-reporting): run the own-Chromium gate against real Windows trees
Nothing on this branch had ever executed on Windows. The unit tests pin the
gate's decision against a mocked taskkill, which cannot show that the decision
does anything to a real process: that `/T /F` reaps a detached grandchild, that
a refusal leaves that tree standing, or that the handle-addressed root kill the
refusal path falls back to reaps the root while orphaning descendants.
Adds a win32-gated live test covering all four, registered in both the
`package_windows` CI lane and `WINDOWS_PACKAGE_TESTS` as
`win32-test-lane-registration` requires.
Also completes the coverage doc's "never instrumented" list, which omitted the
macOS keyboard-input-source probe's POSIX group kill in `ipc/app.ts`.
* fix(crash-reporting): pin the commit-message root kill on the Windows arm
The first Windows run of this branch found nine failures the macOS suite
cannot see: `commit-message-text-generation-test-harness` asserts
`expect(child.kill).not.toHaveBeenCalled()` on `process.platform === 'win32'`,
which is the contract the previous commit deliberately replaced — and it
branches on the real platform, so it is dead code everywhere CI runs today.
The harness now asserts the handle-addressed root kill on every platform. On
win32 it lands after the tree walk, so the expectation waits rather than reading
one tick early, and its ten call sites await it. Red against the pre-fix arm at
all seven sites; the production code is unchanged.
* test(crash-reporting): remove the Windows lane marker tree through the retrying helper
The new win32 spec teardown used a raw rmSync, which the windows-lane-tree-removal
boundary ratchet rejects — and which is exactly the EPERM the ratchet exists to
prevent, since this spec's marker directory is written by processes it has just
force-killed.
* fix(crash-reporting): only refuse pid-addressed tree walks, disclose the handle-less codex site
The own-Chromium gate refused the POSIX process-group arm of
signalProcessTree as well, which was new macOS/Linux behaviour: a stale
getAppMetrics() entry plus pid reuse would orphan a group that main reaps
today. A POSIX group only holds what Orca put in it, so the refusal is now
scoped to win-taskkill-tree and the POSIX arm is recorded and admitted like
the other group kills in main. That also drops the synchronous
getAppMetrics() read from every POSIX termination.
codex-turn-added-roots kills roots found by a table walk, so a refusal has
no handle to fall back to. Pin that the refusal is visible - crumb written,
turn reported as not cancelled - rather than fixing what cannot be fixed.
* test(crash-reporting): detach the Windows survival fixture and observe real spawns
185 lines
6.9 KiB
TypeScript
185 lines
6.9 KiB
TypeScript
import { readFileSync, readdirSync, statSync } from 'node:fs'
|
|
import { join, relative, resolve } from 'node:path'
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
/**
|
|
* The ratchet behind the guard's claim to be a choke point.
|
|
*
|
|
* `admitSelfInitiatedTreeKill` is only "one decision" for as long as every
|
|
* pid-addressed `taskkill /pid <pid> /t /f` in Electron main asks it. Each such
|
|
* kill can land on a recycled pid that is now one of Orca's own Chromium
|
|
* processes (#10680), and an ungated one is also invisible to
|
|
* `selfInitiatedTreeKillCount`, which makes a zero read as exculpatory when it
|
|
* is not. A new family fails here rather than in the field.
|
|
*
|
|
* Exactly what is enforced, so no comment elsewhere claims more: per file, the
|
|
* number of gate admissions must be at least the number of `/pid` call sites.
|
|
* Counting sites rather than files is the point — a file-granular scan would let
|
|
* a second, ungated taskkill land inside a family that already mentions the gate,
|
|
* which is the shape the six highest-risk files now have. What it still cannot
|
|
* see: a site that pairs an ungated kill with a second admission of an already
|
|
* gated one in the same file, and a kill whose `/pid` argument is itself built
|
|
* from a variable.
|
|
*/
|
|
const REPOSITORY_ROOT = resolve(__dirname, '..', '..')
|
|
const MAIN_DIRECTORY = 'src/main/'
|
|
const SCANNED_EXTENSIONS = ['.ts', '.tsx']
|
|
const IGNORED_DIRECTORIES = new Set([
|
|
'node_modules',
|
|
'dist',
|
|
'out',
|
|
'build',
|
|
'.git',
|
|
'__fixtures__'
|
|
])
|
|
|
|
/**
|
|
* One match per call site. Keyed on the `/pid` argument rather than the program
|
|
* name because `/pid <n>` is what makes the kill pid-addressed — it walks
|
|
* whatever tree owns that pid *now* — and because the literal survives a
|
|
* `taskkill` spawned through a constant or a variable, which a quoted-program
|
|
* pattern misses entirely.
|
|
*/
|
|
const PID_ADDRESSED_KILL_SITE = /['"]\/pid['"]/gi
|
|
|
|
/**
|
|
* A call, not an import or a comment: `admitSelfInitiatedTreeKill` in main, and
|
|
* `admitProcessTreeKill` for the `src/shared` seam main installs the same gate
|
|
* into, which shared code cannot import directly.
|
|
*/
|
|
const GATE_ADMISSION = /\badmit(?:SelfInitiatedTreeKill|ProcessTreeKill)\s*\(/g
|
|
|
|
function countMatches(source: string, pattern: RegExp): number {
|
|
return source.match(pattern)?.length ?? 0
|
|
}
|
|
|
|
/** Sites left over once each admission in the file has claimed one. */
|
|
function ungatedKillSiteCount(source: string): number {
|
|
return Math.max(
|
|
countMatches(source, PID_ADDRESSED_KILL_SITE) - countMatches(source, GATE_ADMISSION),
|
|
0
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Only ever shrinks. Each entry states why the gate cannot reach it — never
|
|
* "not got to yet", which is what a new ungated family would also look like.
|
|
*/
|
|
const UNGATED_TASKKILL_ALLOWLIST = new Map<string, string>([
|
|
[
|
|
'src/main/browser/browser-route-egress-electron-launch.ts',
|
|
'Electron probe reached only from *.electron.test.ts; kills the probe Electron it spawned'
|
|
],
|
|
[
|
|
'src/main/browser/browser-route-persisted-worker-electron-process.ts',
|
|
'Electron probe reached only from *.electron.test.ts; kills the probe Electron it spawned'
|
|
],
|
|
[
|
|
'src/cli/handlers/interactive-login-interruption.ts',
|
|
'CLI host: no Chromium pid on the machine to reach, and no reader for the ring'
|
|
],
|
|
[
|
|
'src/relay/subprocess-tree-termination.ts',
|
|
'Relay host: same, and the relay cannot import the main-process gate'
|
|
]
|
|
])
|
|
|
|
function isTestFile(path: string): boolean {
|
|
return /\.(?:test|spec)\.tsx?$/.test(path) || /(?:test-harness|test-fixture|fixture)/.test(path)
|
|
}
|
|
|
|
function scanSourceFiles(directory: string, found: string[] = []): string[] {
|
|
for (const entry of readdirSync(directory)) {
|
|
if (IGNORED_DIRECTORIES.has(entry)) {
|
|
continue
|
|
}
|
|
const path = join(directory, entry)
|
|
if (statSync(path).isDirectory()) {
|
|
scanSourceFiles(path, found)
|
|
continue
|
|
}
|
|
if (SCANNED_EXTENSIONS.some((extension) => entry.endsWith(extension)) && !isTestFile(path)) {
|
|
found.push(path)
|
|
}
|
|
}
|
|
return found
|
|
}
|
|
|
|
// Only the Node-side hosts: a renderer or preload cannot spawn a process at all.
|
|
const SCANNED_HOSTS = ['src/main', 'src/shared', 'src/cli', 'src/relay']
|
|
|
|
/** Scanned once at import: 10k files is seconds, and every case below reuses it. */
|
|
const PID_ADDRESSED_KILL_FILES = SCANNED_HOSTS.flatMap((host) =>
|
|
scanSourceFiles(join(REPOSITORY_ROOT, host))
|
|
.map((path) => ({
|
|
path: relative(REPOSITORY_ROOT, path).split('\\').join('/'),
|
|
source: readFileSync(path, 'utf8')
|
|
}))
|
|
.filter((file) => countMatches(file.source, PID_ADDRESSED_KILL_SITE) > 0)
|
|
)
|
|
|
|
function pidAddressedKillFiles(): { path: string; source: string }[] {
|
|
return PID_ADDRESSED_KILL_FILES
|
|
}
|
|
|
|
describe('main-process tree-kill gate', () => {
|
|
it('finds the taskkill families it is meant to police', () => {
|
|
// Falsifiable: a scanner that matched nothing would pass every case below.
|
|
expect(pidAddressedKillFiles().map((file) => file.path)).toContain(
|
|
'src/main/windows-process-tree-kill.ts'
|
|
)
|
|
})
|
|
|
|
it('routes every pid-addressed taskkill in Electron main through the gate', () => {
|
|
const ungated = pidAddressedKillFiles()
|
|
.filter((file) => file.path.startsWith(MAIN_DIRECTORY))
|
|
.filter((file) => ungatedKillSiteCount(file.source) > 0)
|
|
.map((file) => file.path)
|
|
.filter((path) => !UNGATED_TASKKILL_ALLOWLIST.has(path))
|
|
|
|
expect(ungated).toEqual([])
|
|
})
|
|
|
|
it('leaves no pid-addressed taskkill outside main unaccounted for', () => {
|
|
const unaccounted = pidAddressedKillFiles()
|
|
.filter((file) => !file.path.startsWith(MAIN_DIRECTORY))
|
|
.filter((file) => ungatedKillSiteCount(file.source) > 0)
|
|
.map((file) => file.path)
|
|
.filter((path) => !UNGATED_TASKKILL_ALLOWLIST.has(path))
|
|
|
|
expect(unaccounted).toEqual([])
|
|
})
|
|
|
|
it('counts call sites, not files: a second ungated kill in a gated file is caught', () => {
|
|
// The failure a file-granular scan let through: one gate mention exempting
|
|
// every taskkill in the file.
|
|
const gated = `
|
|
import { admitSelfInitiatedTreeKill } from './own-chromium-tree-kill-guard'
|
|
if (admitSelfInitiatedTreeKill({ pid, site: 's', scope: 'win-taskkill-tree' })) {
|
|
spawn('taskkill', ['/pid', String(pid), '/t', '/f'])
|
|
}
|
|
`
|
|
|
|
expect(ungatedKillSiteCount(gated)).toBe(0)
|
|
expect(
|
|
ungatedKillSiteCount(`${gated}\nspawn('taskkill', ['/pid', String(other), '/t', '/f'])`)
|
|
).toBe(1)
|
|
})
|
|
|
|
it('sees a kill whose program name comes from a constant', () => {
|
|
// A quoted-program pattern misses this shape; the `/pid` argument does not.
|
|
expect(
|
|
ungatedKillSiteCount(`
|
|
const KILLER = 'taskkill'
|
|
spawn(KILLER, ['/pid', String(pid), '/t', '/f'])
|
|
`)
|
|
).toBe(1)
|
|
})
|
|
|
|
it('keeps the allowlist honest: every entry still spawns a taskkill', () => {
|
|
const spawning = new Set(pidAddressedKillFiles().map((file) => file.path))
|
|
|
|
expect([...UNGATED_TASKKILL_ALLOWLIST.keys()].filter((path) => !spawning.has(path))).toEqual([])
|
|
})
|
|
})
|