The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
Merge batch 11-03 be: #2647 #2675 (#2678)
0xken15d9e89e1cdb2b
diff --git a/cli/src/lib/peerAccount.spec.ts b/cli/src/lib/peerAccount.spec.tsindex 3dd3b8939..f1e433594 100644--- a/cli/src/lib/peerAccount.spec.ts+++ b/cli/src/lib/peerAccount.spec.ts@@ -1,6 +1,11 @@+import type { ExecFileException } from 'node:child_process'+import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'import { createServer, connect, type AddressInfo, type Socket } from 'node:net'-import { afterEach, describe, expect, it } from 'vitest'-import { lsofShowsPeer, peerIsThisAccount, portHolderIsThisAccount, procNetListener, procNetOwner, WINDOWS_PEER_LOOKUPS, WINDOWS_PEER_MS, WINDOWS_PEER_QUEUE, windowsListenerQuery, windowsPeerQuery, type WindowsPeerRun } from './peerAccount.js'+import { tmpdir } from 'node:os'+import path from 'node:path'+import { afterEach, describe, expect, it, vi } from 'vitest'+import type { ExecDone } from './patientExec.js'+import { lsofShowsPeer, nativeFirst, peerIsThisAccount, portHolderIsThisAccount, procNetListener, procNetOwner, runWindowsNative, WINDOWS_PEER_LOOKUPS, WINDOWS_PEER_MS, WINDOWS_PEER_NATIVE_MS, WINDOWS_PEER_QUEUE, windowsListenerQuery, windowsPeerQuery, type WindowsNativeRun, type WindowsPeerRun } from './peerAccount.js'describe('who is at the other end of a loopback connection', () => {const opened: Array<() => unknown> = []@@ -192,10 +197,11 @@ describe('who is at the other end of a loopback connection', () => {// Up to four lookups run at once: a sibling's PowerShell or its console host, started by the daemon in the// connection's second, could take an id the caller freed (Remy's review of #2605). Nothing of that kind the daemon// starts calls its port with a secret, so one whose parent or grandparent is the daemon is no.- it('never takes a PowerShell or console host the daemon started, a sibling lookup', () => {+ it('never takes a PowerShell, console host or harness-pty the daemon started, a sibling lookup', () => {const { script } = windowsPeerQuery(50_001, 18473, 42_424)expect(script).toContain('$daemon = [int]$env:HARNESS_DAEMON_PID')- expect(script).toMatch(/\$process\.Name -match '\^\(powershell\|conhost\)\\\.exe\$'/)+ // harness-pty too: each fallback PowerShell runs beside a helper that has just refused the command.+ expect(script).toMatch(/\$process\.Name -match '\^\(powershell\|conhost\|harness-pty\)\\\.exe\$'/)expect(script).toMatch(/\$process\.ParentProcessId -eq \$daemon/)expect(script).toMatch(/\$parent\.ParentProcessId -eq \$daemon/)const sibling = script.indexOf('$daemon'), owner = script.indexOf('GetOwnerSid')@@ -265,6 +271,145 @@ describe('who is at the other end of a loopback connection', () => {})})+ // #2187: the same question, asked of harness-pty in milliseconds. PowerShell took 20 to 25 s on the ARM runner.+ describe('on Windows, by asking harness-pty first', () => {+ const inputs = { HARNESS_PEER_PORT: '50001', HARNESS_OWN_PORT: '18473', HARNESS_DAEMON_PID: '42424' }+ const socket = (remotePort: number) =>+ ({ remoteAddress: '127.0.0.1', remotePort, localAddress: '127.0.0.1', localPort: 18473 }) as Socket+ const powershell = (stdout = 'yes') => {+ const calls: Array<{ script: string; inputs: Record<string, string> }> = []+ const run: WindowsPeerRun = async (script, given) => { calls.push({ script, inputs: given }); return { status: 0, stdout } }+ return { run, calls }+ }+ const native = (answer: Awaited<ReturnType<WindowsNativeRun>>): WindowsNativeRun => async () => answer++ it('takes yes and no from the helper, and never asks PowerShell after them', async () => {+ const ps = powershell('yes')+ expect(await peerIsThisAccount(socket(54_001), 'win32', nativeFirst(native({ status: 0, stdout: 'yes\r\n' }), ps.run))).toBe(true)+ // A no is final: asking again would let a process Windows will not open be judged by another lookup.+ expect(await peerIsThisAccount(socket(54_002), 'win32', nativeFirst(native({ status: 0, stdout: 'no: its owner is another account\r\n' }), ps.run))).toBe(false)+ expect(ps.calls).toEqual([])+ })++ it('hands the helper the connection and asks PowerShell the same question when it is missing or too old', async () => {+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})+ try {+ for (const answer of ['missing', { status: 2, stdout: '' }, { status: 3, stdout: '{"unsupported":true}' }] as const) {+ const ps = powershell('yes')+ const seen: Array<Record<string, string>> = []+ const helper: WindowsNativeRun = async (given) => { seen.push(given); return answer }+ expect(await nativeFirst(helper, ps.run)('the script', inputs), JSON.stringify(answer)).toEqual({ status: 0, stdout: 'yes' })+ expect(seen).toEqual([inputs])+ expect(ps.calls).toEqual([{ script: 'the script', inputs }])+ }+ // Slower a thousand times and nothing else wrong: the log says why, once, not per connection.+ const once = nativeFirst(native('missing'), powershell().run)+ warn.mockClear()+ await once('s', inputs)+ await once('s', inputs)+ expect(warn.mock.calls.flat().join('\n')).toMatch(/harness-pty.*PowerShell/)+ expect(warn).toHaveBeenCalledTimes(1)+ } finally {+ warn.mockRestore()+ }+ })++ // #2608's listener check asks through the same default with its own inputs: the helper only knows a connection, and+ // running it with none would fail with no answer, and the bridge would never move off a held port (Reed's review of #2647).+ it('sends a question the helper does not know to PowerShell, without running the helper or warning', async () => {+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})+ try {+ const ps = powershell('none')+ let ran = 0+ const helper: WindowsNativeRun = async () => { ran++; return { status: 0, stdout: 'yes' } }+ const other = { HARNESS_LISTEN_PORT: '51794' }+ expect(await nativeFirst(helper, ps.run)('another script', other)).toEqual({ status: 0, stdout: 'none' })+ expect(ran).toBe(0)+ expect(ps.calls).toEqual([{ script: 'another script', inputs: other }])+ expect(warn).not.toHaveBeenCalled()+ } finally {+ warn.mockRestore()+ }+ })++ // The bridge's real listener check (#2608), through the path its default takes.+ it('keeps the bridge\'s listener check on PowerShell, with its own query, without running the helper', async () => {+ const ps = powershell('no')+ let ran = 0+ const helper: WindowsNativeRun = async () => { ran++; return { status: 0, stdout: 'yes' } }+ expect(await portHolderIsThisAccount(51_794, 'win32', nativeFirst(helper, ps.run))).toBe(false)+ expect(ran).toBe(0)+ expect(ps.calls).toEqual([windowsListenerQuery(51_794)])+ })++ it('says no without PowerShell for a helper that hangs, crashes or says anything else', async () => {+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})+ try {+ const answers = [{ status: null, stdout: '' }, { status: 1, stdout: '' }, { status: 101, stdout: 'yes' }, { status: 3221225477, stdout: '' }, { status: 0, stdout: 'yes yes' }, { status: 0, stdout: '' }]+ for (const [i, answer] of answers.entries()) {+ const ps = powershell('yes')+ expect(await peerIsThisAccount(socket(54_100 + i), 'win32', nativeFirst(native(answer), ps.run)), JSON.stringify(answer)).toBe(false)+ expect(ps.calls).toEqual([])+ }+ } finally {+ warn.mockRestore()+ }+ })++ const exec = (error: ExecFileException | null, stdout = '') => {+ const calls: Array<{ file: string; args: readonly string[]; options: Record<string, unknown> }> = []+ const run = (file: string, args: readonly string[], options: object, done: ExecDone) => {+ calls.push({ file, args, options: options as Record<string, unknown> })+ done(error, stdout, '')+ }+ return { run, calls }+ }+ const failed = (code: string | number | null): ExecFileException => Object.assign(new Error('failed'), { code })++ it('runs the helper with the ports and the daemon as arguments, hidden, and bounded', async () => {+ const ran = exec(null, 'yes\r\n')+ expect(await runWindowsNative(inputs, () => 'C:\\pty\\harness-pty.exe', ran.run)).toEqual({ status: 0, stdout: 'yes\r\n' })+ expect(ran.calls).toEqual([{ file: 'C:\\pty\\harness-pty.exe',+ args: ['peer-account', '--peer-port', '50001', '--own-port', '18473', '--daemon-pid', '42424'],+ options: expect.objectContaining({ timeout: WINDOWS_PEER_NATIVE_MS, windowsHide: true }) }])+ expect(WINDOWS_PEER_NATIVE_MS).toBeGreaterThanOrEqual(1_000)+ expect(WINDOWS_PEER_NATIVE_MS).toBeLessThan(WINDOWS_PEER_MS)+ })++ it('reads missing only when there is no file to run, and every other failure as a failure', async () => {+ const file = () => 'harness-pty.exe'+ expect(await runWindowsNative(inputs, () => null, exec(null).run)).toBe('missing')+ expect(await runWindowsNative(inputs, () => { throw new Error('no runtime') }, exec(null).run)).toBe('missing')+ expect(await runWindowsNative(inputs, file, exec(failed('ENOENT')).run)).toBe('missing')+ expect(await runWindowsNative(inputs, file, exec(failed(2)).run)).toEqual({ status: 2, stdout: '' })+ expect(await runWindowsNative(inputs, file, exec(failed(3), '{"unsupported":true}').run)).toEqual({ status: 3, stdout: '{"unsupported":true}' })+ expect(await runWindowsNative(inputs, file, exec(failed(3221225477)).run)).toEqual({ status: 3221225477, stdout: '' })+ // Past its limit: the "did not say in time" answer.+ expect(await runWindowsNative(inputs, file, exec(failed('ETIMEDOUT')).run)).toEqual({ status: null, stdout: '' })+ // A file there that will not run is not an older build: no, and no second lookup.+ expect(await runWindowsNative(inputs, file, exec(failed('EACCES')).run)).toEqual({ status: 1, stdout: '' })+ expect(await runWindowsNative(inputs, file, exec(failed(null)).run)).toEqual({ status: 1, stdout: '' })+ })++ // The default, whole: the listeners name no lookup, so this is the path a real connection takes.+ it.skipIf(process.platform === 'win32')('asks the installed helper by default, and needs no PowerShell', async () => {+ const dir = mkdtempSync(path.join(tmpdir(), 'peer-native-'))+ const was = process.env.HARNESS_PTY_BIN+ try {+ const helper = path.join(dir, 'harness-pty')+ writeFileSync(helper, '#!/bin/sh\necho "$@" > "$0.args"\necho yes\n')+ chmodSync(helper, 0o755)+ process.env.HARNESS_PTY_BIN = helper+ expect(await peerIsThisAccount(socket(54_200), 'win32')).toBe(true)+ expect(readFileSync(`${helper}.args`, 'utf8').trim()).toBe(`peer-account --peer-port 54200 --own-port 18473 --daemon-pid ${process.pid}`)+ } finally {+ if (was === undefined) delete process.env.HARNESS_PTY_BIN+ else process.env.HARNESS_PTY_BIN = was+ rmSync(dir, { recursive: true, force: true })+ }+ })+ })+it('reads the owner of the client end from /proc/net/tcp, not the server end', () => {const table = [' sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode',diff --git a/cli/src/lib/peerAccount.ts b/cli/src/lib/peerAccount.tsindex 0773cddd7..037b6b7bc 100644--- a/cli/src/lib/peerAccount.ts+++ b/cli/src/lib/peerAccount.ts@@ -12,6 +12,8 @@import { execFile } from 'node:child_process'import { readFile } from 'node:fs/promises'import type { Socket } from 'node:net'+import { patientExec, type ExecDone, type PatientExecOptions } from './patientExec.js'+import { ptyHostBinary } from './ptyHost/launch.js'import { runPowershellAsync } from './windows/powershell.js'export type PeerAccount = (socket: Socket) => Promise<boolean>@@ -110,14 +112,67 @@ export async function portHolderIsThisAccount(port: number, platform: NodeJS.Pla/*** Windows PowerShell's start and its CIM queries: about a second on x64, 20 to 25 s on the Windows ARM runner (the- * Windows runs on #2605), so a slow or busy PC is given 45 s. A lookup past that is no for its connection. A native lookup- * in harness-pty, in milliseconds, is its own task.+ * Windows runs on #2605), so a slow or busy PC is given 45 s. A lookup past that is no for its connection. It runs only+ * for a host too old to answer (see [nativeFirst]).*/export const WINDOWS_PEER_MS = 45_000-/** How the daemon runs [windowsPeerQuery]: Windows PowerShell, off its event loop. Tests give their own. */+/** How the daemon asks: the script is [windowsPeerQuery]'s, for PowerShell, and its inputs name the connection. Tests give their own. */export type WindowsPeerRun = (script: string, inputs: Record<string, string>) => Promise<{ status: number | null; stdout: string }>-const runWindowsPeer: WindowsPeerRun = (script, inputs) => runPowershellAsync(script, inputs, WINDOWS_PEER_MS)++/**+ * `harness-pty peer-account` takes milliseconds (#2187). A helper that takes this long is hung: its connection is refused,+ * and the client can retry. Far more than the second the real Windows run allows, for a PC under load.+ */+export const WINDOWS_PEER_NATIVE_MS = 5_000++/** The helper's exit and output, or `missing` when there is no file to run. */+export type WindowsNativeRun = (inputs: Record<string, string>) => Promise<{ status: number | null; stdout: string } | 'missing'>+type NativeExec = (file: string, args: readonly string[], options: PatientExecOptions, done: ExecDone) => void+const nativeExec: NativeExec = patientExec(execFile)++/** Runs the helper for [inputs], the connection [windowsPeerQuery] names. A file that is there and will not run is a failed lookup, not a missing one. */+export function runWindowsNative(inputs: Record<string, string>, binary: () => string | null = ptyHostBinary, exec: NativeExec = nativeExec): ReturnType<WindowsNativeRun> {+ let file: string | null+ try {+ file = binary()+ } catch {+ file = null+ }+ if (!file) return Promise.resolve('missing')+ const args = ['peer-account', '--peer-port', inputs.HARNESS_PEER_PORT!, '--own-port', inputs.HARNESS_OWN_PORT!, '--daemon-pid', inputs.HARNESS_DAEMON_PID!]+ return new Promise((resolve) => {+ exec(file, args, { timeout: WINDOWS_PEER_NATIVE_MS, maxBuffer: 64 * 1024, windowsHide: true }, (error, stdout) => {+ const code = error ? error.code : 0+ if (code === 'ENOENT') resolve('missing')+ else if (code === 'ETIMEDOUT') resolve({ status: null, stdout: '' })+ else resolve({ status: typeof code === 'number' ? code : 1, stdout })+ })+ })+}++/**+ * Asks the helper, and PowerShell only when the helper cannot be asked: it is missing, or a host older than this CLI says+ * 2 for a command it does not know (or 3, unsupported). Its yes and no are final, and so is a hang or a crash: PowerShell+ * after a no would be a second chance for a caller the helper refused, and after a hang 45 s more for each connection.+ */+export function nativeFirst(native: WindowsNativeRun, powershell: WindowsPeerRun): WindowsPeerRun {+ let told = false+ return async (script, inputs) => {+ // The helper answers only who owns a connection. Another query through this default, as the listener check of #2608, has+ // its own script and rules: run with no ports, the helper could say nothing, and that check would go quiet.+ if (inputs.HARNESS_PEER_PORT === undefined) return powershell(script, inputs)+ const answer = await native(inputs)+ if (answer !== 'missing' && answer.status !== 2 && answer.status !== 3) return answer+ if (!told) {+ told = true+ console.warn('[peer] harness-pty cannot say who is calling (missing or too old): asking PowerShell, which is far slower')+ }+ return powershell(script, inputs)+ }+}++const runWindowsPeer: WindowsPeerRun = nativeFirst(runWindowsNative, (script, inputs) => runPowershellAsync(script, inputs, WINDOWS_PEER_MS))/*** Windows: whether the client end of the connection 127.0.0.1:[peerPort] → 127.0.0.1:[ownPort] belongs to a process of@@ -140,18 +195,18 @@ export function windowsPeerQuery(peerPort: number, ownPort: number, daemonPid: n// connection's time to the second and the process's finer (the Windows run on #2605 read a process started at// 13:32:35 as after its connection of 13:32:35), so the process's time is cut to the second and compared with no// slack. Slack let the lookup's own PowerShell, started just after, take the freed id and pass (Remy's review). A- // reuse within the connection's own second is left to the native lookup.+ // reuse within the connection's own second stays open here; the native lookup compares finer where Windows gives it."if (-not $process.CreationDate -or -not $rows[0].CreationTime) { 'no: Windows gave no start time'; exit 0 }",'$started = $process.CreationDate.AddTicks(-($process.CreationDate.Ticks % [TimeSpan]::TicksPerSecond))',"if ($started -gt $rows[0].CreationTime) { 'no: its process started ' + $process.CreationDate.ToString('o') + ', the connection ' + $rows[0].CreationTime.ToString('o'); exit 0 }",- // Nor another lookup running at once: a PowerShell the daemon started, or that PowerShell's console host, started in- // the connection's second, could have taken the freed id (Remy's review of #2605). Nothing of that kind the daemon- // starts calls its port with a secret.+ // Nor another lookup running at once: a PowerShell the daemon started, the helper that ran before it, or a console+ // host of either, started in the connection's second, could have taken the freed id (Remy's review of #2605). Nothing+ // of that kind the daemon starts calls its port with a secret.'$daemon = [int]$env:HARNESS_DAEMON_PID',- "if ($process.Name -match '^(powershell|conhost)\\.exe$') {",- " if ($process.ParentProcessId -eq $daemon) { 'no: a PowerShell the daemon started owns it'; exit 0 }",+ "if ($process.Name -match '^(powershell|conhost|harness-pty)\\.exe$') {",+ " if ($process.ParentProcessId -eq $daemon) { 'no: a ' + $process.Name + ' the daemon started owns it'; exit 0 }",' $parent = Get-CimInstance Win32_Process -Filter ("ProcessId = " + [int]$process.ParentProcessId)',- " if ($parent -and $parent.ParentProcessId -eq $daemon) { 'no: a console host of a PowerShell the daemon started owns it'; exit 0 }",+ " if ($parent -and $parent.ParentProcessId -eq $daemon) { 'no: a ' + $process.Name + ' the daemon started owns it'; exit 0 }",'}','$owner = Invoke-CimMethod -InputObject $process -MethodName GetOwnerSid',"if ($owner.ReturnValue -eq 0 -and $owner.Sid -and $owner.Sid -eq [Security.Principal.WindowsIdentity]::GetCurrent().User.Value) { 'yes' } else { 'no: its owner is ' + $owner.Sid + ' (' + $owner.ReturnValue + ')' }",@@ -207,7 +262,7 @@ async function askWindowsListener(port: number, run: WindowsPeerRun): Promise<bo}/**- * How many lookups run at once. Each is a PowerShell in this account for up to WINDOWS_PEER_MS, and whoever heard the+ * How many lookups run at once. Each is a process in this account, a PowerShell for up to WINDOWS_PEER_MS, and whoever heard the* hook token could open connections without end (Luna's review of #2605). Past these a lookup waits its turn, so the* owner's own burst of new connections (an agent's MCP clients on the bridge at start) is answered, not told "unknown* address" (Reed's review). Only past the queue too is it no, for now: that no is not kept on the connection, so adiff --git a/cli/src/lib/windows/peerAccount.windows.real.spec.ts b/cli/src/lib/windows/peerAccount.windows.real.spec.tsindex 5243029f2..5ac5f8ab0 100644--- a/cli/src/lib/windows/peerAccount.windows.real.spec.ts+++ b/cli/src/lib/windows/peerAccount.windows.real.spec.ts@@ -2,10 +2,11 @@// connection this process made is this account's. Windows only; the Windows workflow runs it with src/lib/windows.// Another account's connection cannot be made from one account's test run; that answer is pinned by the unit spec. The yes// below also shows Windows gives both times the check compares: the process started before its connection was made.+// The workflow names the host it built in HARNESS_PTY_BIN, so these run the native lookup; PowerShell has its own case.import { spawn } from 'node:child_process'import { connect, createServer, type AddressInfo, type Socket } from 'node:net'import { afterEach, describe, expect, it } from 'vitest'-import { peerIsThisAccount, windowsPeerQuery } from '../peerAccount.js'+import { peerIsThisAccount, runWindowsNative, WINDOWS_PEER_MS, windowsPeerQuery } from '../peerAccount.js'import { runPowershellAsync } from './powershell.js'const WINDOWS = process.platform === 'win32'@@ -55,4 +56,51 @@ describe.runIf(WINDOWS)('who is calling, asked of Windows', () => {cleanup.push(() => socket.destroy())if (!await peerIsThisAccount(socket, 'win32')) expect.fail(await asked(socket))}, 240_000)++ /** A connection of this process's: its accepted end, and the client to drop. */+ const connection = async () => {+ const server = createServer()+ cleanup.push(() => server.close())+ await new Promise<void>((resolve) => { server.listen(0, '127.0.0.1', resolve) })+ const accepted = new Promise<Socket>((resolve) => { server.once('connection', resolve) })+ const client = connect((server.address() as AddressInfo).port, '127.0.0.1')+ cleanup.push(() => client.destroy())+ const socket = await accepted+ cleanup.push(() => socket.destroy())+ return { socket, client }+ }++ // Acceptance of #2187: under a second on x64 and ARM, where PowerShell took about 1 s and 20 to 25 s. The helper itself+ // is asked, so a lookup that fell back to PowerShell cannot pass here, and each time is printed for the run's log.+ it('answers by the native helper in under a second, yes and then no', async () => {+ for (let i = 0; i < 5; i++) {+ const { socket, client } = await connection()+ const query = windowsPeerQuery(socket.remotePort!, socket.localPort!)+ const start = performance.now()+ const yes = await runWindowsNative(query.inputs)+ const took = performance.now() - start+ console.log(`[peer] native lookup ${i + 1}: ${Math.round(took)} ms`)+ expect(yes, 'the helper answered').toEqual({ status: 0, stdout: expect.stringMatching(/^yes\r?\n$/) })+ expect(took).toBeLessThan(1_000)++ await new Promise<void>((resolve) => { client.once('close', () => resolve()); client.destroy() })+ const start2 = performance.now()+ const no = await runWindowsNative(query.inputs)+ expect(performance.now() - start2).toBeLessThan(1_000)+ expect(no).toEqual({ status: 0, stdout: expect.stringMatching(/^no: 0 connections match\r?\n$/) })+ }+ }, 120_000)++ it('answers a real connection through the default path in under a second', async () => {+ const { socket } = await connection()+ const start = performance.now()+ expect(await peerIsThisAccount(socket, 'win32')).toBe(true)+ expect(performance.now() - start).toBeLessThan(1_000)+ }, 60_000)++ // The fallback for an older host stays real too.+ it('still gets the same yes from PowerShell, for an older host', async () => {+ const { socket } = await connection()+ if (!await peerIsThisAccount(socket, 'win32', (script, inputs) => runPowershellAsync(script, inputs, WINDOWS_PEER_MS))) expect.fail(await asked(socket))+ }, 240_000)})diff --git a/cli/src/superTabs/machines.spec.ts b/cli/src/superTabs/machines.spec.tsindex 5d47d141a..d7648f648 100644--- a/cli/src/superTabs/machines.spec.ts+++ b/cli/src/superTabs/machines.spec.ts@@ -2,12 +2,15 @@ import { execFileSync } from 'node:child_process'import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs'import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'import { homedir, tmpdir } from 'node:os'-import { join } from 'node:path'+import { dirname, join } from 'node:path'import { afterEach, describe, expect, it, onTestFinished, vi } from 'vitest'import type { RegisteredSession } from '../lib/registry.js'import { fakeCore } from '../testing/fakeCore.js'import { apisOn, ContributedMachines, createQuietGit, liveOn, localModelsStamp, offered, READINGS_RETRY_MS, refusedOn, servedOn, sharedWith, swarmUnconfirmedFile, swarmWorktreesFile } from './machines.js'import { SuperTabHostError } from './host.js'+import { createSwarmAgents } from './inCore/agents.js'+import { createConfidentialPurge, purgeRecord } from './inCore/teamPurge.js'+import { PM_HOMES } from './teamDaemon.js'import { startAgent } from './start.js'import { hostForm, swarmReply } from './fixtures/read/index.js'import { HostFeed } from './hostFeed.js'@@ -868,6 +871,42 @@ describe('contributed machine executor', () => {}const settle = () => new Promise(resolve => setTimeout(resolve, 0))+ // Task #2261, security review 1 of #2668: another coordinator was set while this one started, so the server refused its+ // join. The start's own close deletes nothing, so the agent's data has to go the way a failed start's does.+ describe('because another coordinator was set', () => {+ const REASON = 'This project already has Mine as its Product Manager.'+ const refusedStart = { ok: false, error: REASON, refused: REASON, startId: 'start', machineId: 'host', creationId: 'creation', harnessId: 'launched' }++ it('removes a Team account\'s engine home for it, through the Swarm\'s own close', async () => {+ const home = mkdtempSync(join(tmpdir(), 'refused-start-'))+ onTestFinished(() => rmSync(home, { recursive: true, force: true }))+ const own = join(home, PM_HOMES, 'super-start', 'codex')+ mkdirSync(own, { recursive: true })+ const purge = async (request: Record<string, unknown>) => request.mode === 'inspect' ? { reviewId: 'review' } : { ok: true }+ const ended = createConfidentialPurge({ confidential: () => true, home, record: purgeRecord(join(home, 'purges.json')), purge,+ session: agentId => ({ agentId, sessionId: 'session', registeredAt: 1, engine: 'codex', cwd: null, codexHome: own }) })+ const { f, fail } = failedLater(() => ({ error: REASON }))+ f.core.agents.closeMember = createSwarmAgents({ byAgent: () => null, stoppedIds: () => ['launched'], closeAgent: async () => ({ closed: true }), purgeEnded: ended,+ members: { leads: () => new Set(), harnesses: () => new Set() } } as never).closeMember as never+ f.start.mockResolvedValue(refusedStart as never)+ await f.machine.tick()+ fail(); f.advance(); await f.machine.tick(); await settle(); await vi.waitFor(() => expect(existsSync(dirname(own))).toBe(false))+ })++ it('tries again when the purge fails, and stops once it has gone', async () => {+ const { f, closeMember, fail } = failedLater(() => ({ error: REASON }))+ closeMember.mockResolvedValueOnce({ error: 'PURGE_FAILED', detail: 'Something still names the session' })+ f.start.mockResolvedValue(refusedStart as never)+ await f.machine.tick()+ fail(); f.advance(); await f.machine.tick(); await settle()+ expect(closeMember).toHaveBeenCalledWith('launched', 'creation')+ f.advance(); await f.machine.tick(); await settle()+ expect(closeMember).toHaveBeenCalledTimes(2)+ f.advance(); await f.machine.tick(); await settle()+ expect(closeMember).toHaveBeenCalledTimes(2)+ })+ })+it('stops the agent it launched for it, by the start\'s own creation receipt', async () => {const { f, closeMember, fail } = failedLater(() => ({ error: 'Mac Auto did not start it within 3 minutes' }))await f.machine.tick()diff --git a/cli/src/superTabs/machines.ts b/cli/src/superTabs/machines.tsindex cba21b735..c6081e97f 100644--- a/cli/src/superTabs/machines.ts+++ b/cli/src/superTabs/machines.ts@@ -680,8 +680,10 @@ export class ContributedMachines {// Launched, and its creation still under way on the daemon: anything else is not preparing.if (result.error === 'START_PENDING' && result.state === 'pending') this.preparing.add(String(start.id))else this.preparing.delete(String(start.id))- // Launched, its engine yet to record its session: if the Swarm fails this start meanwhile, [abandoned] stops it.- if (result.error === 'START_PENDING' && identifier(result.harnessId) && identifier(result.creationId)) {+ // Launched, its engine yet to record its session: if the Swarm fails this start meanwhile, [abandoned] stops it. So+ // with a join the server refused (backend leadStart.ts leadOvertaken): the start's own close deletes nothing, and+ // this close runs a Team account's confidential purge, and tries again until it has (security review 1 of #2668).+ if ((result.error === 'START_PENDING' || typeof result.refused === 'string') && identifier(result.harnessId) && identifier(result.creationId)) {this.remember(String(start.id), { path, harnessId: result.harnessId, creationId: result.creationId, tries: 0, checking: false })} else this.forget(String(start.id))if (!refused) this.refusedSince.delete(start.id)diff --git a/cli/src/superTabs/start.spec.ts b/cli/src/superTabs/start.spec.tsindex d9a7e2e7e..1285d6228 100644--- a/cli/src/superTabs/start.spec.ts+++ b/cli/src/superTabs/start.spec.ts@@ -87,7 +87,7 @@ describe('a join the server refused', () => {const result = await onFakeClock(() => startAgent({ action: 'resume', tabId: 'tab', startId: 'start' }, f.scoped, where, f.open))expect(closes(f)).toEqual([['agent_close', { agentId: 'native', mode: 'now' }, expect.any(Number)]])expect(switchCall(f)).toBeUndefined()- expect(result).toMatchObject({ ok: false, error: REASON, startId: 'start', harnessId: 'native', refused: REASON })+ expect(result).toMatchObject({ ok: false, error: REASON, startId: 'start', creationId: 'super-native-receipt', harnessId: 'native', refused: REASON })expect(result).not.toHaveProperty('agentId')})diff --git a/cli/src/superTabs/start.ts b/cli/src/superTabs/start.tsindex 7a7557ad0..bcc41941f 100644--- a/cli/src/superTabs/start.ts+++ b/cli/src/superTabs/start.ts@@ -539,7 +539,7 @@ async function startOnce(input: Json, scoped: ScopedRequest, where: Where, open:const refused = text(object(joined.result).refused)if (refused) {const closed = await target.request('agent_close', { agentId: nativeId, mode: 'now' }, 60_000).catch((): Json => ({}))- return { ok: false, error: refused, refused, startId, machineId: request.machineId, harnessId: nativeId, ...(closed.closed === true ? {} : { closeFailed: true }) }+ return { ok: false, error: refused, refused, startId, machineId: request.machineId, creationId, harnessId: nativeId, ...(closed.closed === true ? {} : { closeFailed: true }) }}}await target.request('super_tabs', { action: 'wake' }).catch(() => {})diff --git a/docs/plans/2026-11-02-windows-native-peer-lookup-plan.md b/docs/plans/2026-11-02-windows-native-peer-lookup-plan.mdnew file mode 100644index 000000000..a8bb68cfb--- /dev/null+++ b/docs/plans/2026-11-02-windows-native-peer-lookup-plan.md@@ -0,0 +1,69 @@+# Windows: a native account lookup for local connections: Implementation Plan++> Way of working: this project's instructions. The Engineer implements it task by task, each failing test committed before its code. Security Reviewers: Reed and Orion. The PM starts the real Windows run on the final head.++**Goal:** The check of who is calling over loopback runs natively in `harness-pty` in milliseconds, keeping W1's answers and fail-closed rules, with PowerShell only if the helper is missing.++**Architecture:** A new `harness-pty peer-account` command reads the connection's row (`GetExtendedTcpTable`, `TCP_TABLE_OWNER_MODULE_CONNECTIONS`, which gives its owner and creation time), then the owner process's creation time and token. A pure `verdict` holds the rules, so they run on every platform's tests; a thin Windows layer gathers the facts. `peerAccount.ts` runs it, one process per new connection, as it runs PowerShell.++**Tech Stack:** Rust with `windows-sys` (`pty/`), TypeScript (`cli/`).++**Spec:** Task #2187; W1 (#2605) and `docs/security/2026-11-02-team-account-windows.md`.++## Global Constraints++- Same answers as W1: exactly one established row of the exact pair 127.0.0.1:peer to 127.0.0.1:own; owner compared by SID, never by name; a process gone, unreadable, or started after its connection is no; anything that fails is no.+- The helper's no is final: PowerShell runs only if the helper is missing or too old to know the command, never to ask again after a no.+- Ports and pids are arguments, checked as numbers.+- PR body starts: `Requested by [email protected] through Harness.`+- Two independent security reviews. The real Windows run, x64 and ARM, on the final head.++## Review Focus++- A recycled process id: the process now holding it started after the connection. Answer no, to the finest time Windows gives.+- The lookup itself, or another lookup at once, taking a freed id. Answer no.+- A process of another account whose token Windows will not open. Answer no, and no PowerShell after it.+- An older `harness-pty` beside a new CLI: it refuses the unknown command, and PowerShell answers as before.+- A helper that hangs or dies: its connection is refused, not left waiting.++## Design++- **Growth:** N/A because nothing is stored.+- **Concurrency:** W1's limit of lookups at once and its queue stay. Each lookup is one short process.+- **Read cost:** one read of the TCP table and one process's facts per new authenticated connection, asked only after the secret matched.+- **Scale (10x):** a table of tens of thousands of rows is still read once per new connection. What warns first: the lookup's log line and its time in the real Windows spec.+- **Compatibility:** an older `harness-pty` falls back to PowerShell. Other platforms answer `unsupported` and keep their own lookups.+- **Migration + rollback:** none. Rolling back restores PowerShell.+- **Audience:** N/A because it answers only yes or no to the daemon that started it.+- **Identity:** compares the caller's SID with this process's own; never a name.+- **Failure:** every failure is no, logged with its reason.+- **Platforms:** Windows x64 and ARM; elsewhere the command says unsupported.++---++### Task 1: The rules, in `harness-pty`++**Files:**+- Create: `pty/src/peer.rs`+- Modify: `pty/src/cli.rs`, `pty/src/lib.rs`, `pty/src/ps.rs`, `pty/Cargo.toml`++**Interfaces:**+- Produces: `harness-pty peer-account --peer-port N --own-port N --daemon-pid N`, printing `yes` or `no: <why>`, exit 0. Exit 2 for usage or an older binary's unknown command; exit 3 unsupported.++- [ ] **Step 1: Commit failing tests** of the pure `verdict`, one per rule above, and of the command's arguments.+- [ ] **Step 2: Watch them fail.**+- [ ] **Step 3: Commit the code**, then the Windows layer, cross-checked for x64 and ARM.++### Task 2: `peerAccount.ts` asks it++**Files:**+- Modify: `cli/src/lib/peerAccount.ts`, `cli/src/lib/peerAccount.spec.ts`, `cli/src/lib/windows/peerAccount.windows.real.spec.ts`++**Interfaces:**+- Consumes: the command above; `ptyHostBinary()`.++- [ ] **Step 1: Commit failing tests:** yes and no are taken from the helper; PowerShell runs only when the helper is missing or says 2 or 3; a hang or crash is no, without PowerShell; the real spec measures each lookup under 1 s.+- [ ] **Step 2: Watch them fail.**+- [ ] **Step 3: Commit the code. Mark the pull request ready; message Reed and Orion in their steps; ask the PM for the real Windows run.**++Found while wiring: the helper refused only its own kind of process the daemon started, and W1's query only PowerShell and console hosts. A fallback PowerShell runs beside a helper that just refused the command, so both now refuse all three, with the same parent and grandparent rule.diff --git a/docs/security/2026-11-02-team-account-windows.md b/docs/security/2026-11-02-team-account-windows.mdindex ddbb89f8f..6040a4207 100644--- a/docs/security/2026-11-02-team-account-windows.md+++ b/docs/security/2026-11-02-team-account-windows.md@@ -61,16 +61,19 @@ make the daemon refuse the credential over TCP from another account. Windows can(`GetExtendedTcpTable` gives the owning process, whose token names its account).**Fixed (task #2095).** All three parts, each test first; the W1 tests above are turned around:-- The daemon asks Windows who owns the calling end of a TCP connection that showed a valid secret (`lib/peerAccount.ts`- `windowsPeerQuery`: `Get-NetTCPConnection`, then the process's owner SID against this account's). Anything but a clear yes is- no. This alone makes a credential heard on the port useless to another account, from an old client or plugin too.- It takes only a process that started before the connection was made, compared in whole seconds (the connection's time comes- only to the second), never the lookup's own process, and never a PowerShell or console host the daemon started (another- lookup running at once). A process id taken within the connection's own second by some other process of the owner's is- still open until the native lookup. At most four+- The daemon asks Windows who owns the calling end of a TCP connection that showed a valid secret: `harness-pty peer-account`+ (`pty/src/peer.rs`, the TCP table, then the process's token), in milliseconds, or for a host too old to know that command,+ `lib/peerAccount.ts` `windowsPeerQuery` (`Get-NetTCPConnection`, then the process's owner SID against this account's). The two+ apply the same rules. Anything but a clear yes is no, and the helper's no is final. This alone makes a credential heard on the+ port useless to another account, from an old client or plugin too.+ It takes only a process that started before the connection was made, never the lookup's own process, and never a harness-pty,+ PowerShell or console host the daemon started (another lookup running at once). PowerShell compares in whole seconds, as it gets+ the connection's time only to the second, so a process id taken within that second by some other process of the owner's stays+ open on an older host. The helper compares exactly when Windows gives the connection's time finer than a second, and refuses a time later than+ the clock, so a time misread can only fail closed. At most fourlookups run at once and up to 32 wait their turn; one past those is no for now, and is asked again on its next request. A- lookup that times out (45 s: about a second on x64, 20 to 25 s on the Windows ARM runner) stays no for that connection; a- native lookup in harness-pty is its own task.+ lookup that times out (5 s for the helper; 45 s for PowerShell, about a second on x64 and 20 to 25 s on the Windows ARM runner)+ stays no for that connection.- Hooks post over the daemon's pipe (`hook/notify.mjs` `windowsDaemonPipe`), proving the token without sending it, and take ananswer only with the daemon's proof. With no live pipe they post nothing, as on macOS with no socket.- CLI clients never put the credential on TCP (`localCredentialHeaders` gives nothing on Windows). Their fetch helper and theirdiff --git a/pty/Cargo.toml b/pty/Cargo.tomlindex c89a4f7bf..44398a2ff 100644--- a/pty/Cargo.toml+++ b/pty/Cargo.toml@@ -38,7 +38,7 @@ sha2 = "0.10"libc = "0.2"[target.'cfg(windows)'.dependencies]-windows-sys = { version = "0.59", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_Security", "Win32_Security_Authorization", "Win32_Storage_FileSystem", "Win32_System_Pipes", "Win32_System_Com", "Win32_System_Console", "Win32_System_IO", "Win32_System_JobObjects", "Win32_System_Kernel", "Win32_System_LibraryLoader", "Win32_System_Threading", "Win32_System_Diagnostics_ToolHelp", "Win32_System_ProcessStatus", "Win32_System_Registry", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging", "Wdk_System_Threading"] }+windows-sys = { version = "0.59", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_NetworkManagement_IpHelper", "Win32_NetworkManagement_Ndis", "Win32_Networking_WinSock", "Win32_Security", "Win32_Security_Authorization", "Win32_Storage_FileSystem", "Win32_System_Pipes", "Win32_System_Com", "Win32_System_Console", "Win32_System_IO", "Win32_System_JobObjects", "Win32_System_Kernel", "Win32_System_LibraryLoader", "Win32_System_Threading", "Win32_System_Diagnostics_ToolHelp", "Win32_System_ProcessStatus", "Win32_System_Registry", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging", "Wdk_System_Threading"] }# Its own stand-in app, so this integration executable supplies main.[[test]]diff --git a/pty/src/cli.rs b/pty/src/cli.rsindex 43140a660..658a224fd 100644--- a/pty/src/cli.rs+++ b/pty/src/cli.rs@@ -42,6 +42,11 @@ pub enum Command {reexec: bool,},Version,+ PeerAccount {+ peer_port: u16,+ own_port: u16,+ daemon_pid: u32,+ },Ps,}@@ -53,6 +58,29 @@ pub fn parse(args: &[String]) -> Result<Command, String> {Some("--json") => Ok(Command::Ps),_ => Err("ps needs --json".into()),},+ Some("peer-account") => {+ let (mut peer_port, mut own_port, mut daemon_pid) = (None, None, None);+ while let Some(arg) = args.next() {+ let value = args.next().ok_or("peer-account flags need values")?;+ let bad = || format!("peer-account: {arg} takes a number");+ match arg {+ "--peer-port" => peer_port = Some(value.parse().map_err(|_| bad())?),+ "--own-port" => own_port = Some(value.parse().map_err(|_| bad())?),+ "--daemon-pid" => daemon_pid = Some(value.parse().map_err(|_| bad())?),+ _ => return Err(USAGE.into()),+ }+ }+ match (peer_port, own_port, daemon_pid) {+ (Some(peer_port @ 1..), Some(own_port @ 1..), Some(daemon_pid)) => {+ Ok(Command::PeerAccount {+ peer_port,+ own_port,+ daemon_pid,+ })+ }+ _ => Err("peer-account needs --peer-port, --own-port and --daemon-pid".into()),+ }+ }Some("serve") => {let mut home = None;let mut history_limit = DEFAULT_HISTORY_LIMIT;@@ -89,6 +117,11 @@ pub fn main() {match parse(&args) {Ok(Command::Version) => println!("{}", version_line()),Ok(Command::Ps) => ps(),+ Ok(Command::PeerAccount {+ peer_port,+ own_port,+ daemon_pid,+ }) => peer_account(peer_port, own_port, daemon_pid),Ok(Command::Serve {home,history_limit,@@ -116,6 +149,22 @@ pub fn main() {}}+/// `peer-account`: `yes` or `no: <why>` on Windows; elsewhere exit 3, where the daemon has its own+/// lookup (cli/src/lib/peerAccount.ts).+fn peer_account(peer_port: u16, own_port: u16, daemon_pid: u32) {+ #[cfg(windows)]+ match crate::peer::ask(peer_port, own_port, daemon_pid) {+ crate::peer::Verdict::Yes => println!("yes"),+ crate::peer::Verdict::No(why) => println!("no: {why}"),+ }+ #[cfg(not(windows))]+ {+ let _ = (peer_port, own_port, daemon_pid);+ println!("{{\"unsupported\":true}}");+ std::process::exit(3);+ }+}+/// `ps --json`: `{"rows":[…]}` on Windows; elsewhere `{"unsupported":true}`, exit 3 (the daemon/// reads `ps` there).fn ps() {@@ -342,6 +391,59 @@ mod tests {fn parses_commands() {assert_eq!(parse(&args(&["version"])), Ok(Command::Version));assert_eq!(parse(&args(&["ps", "--json"])), Ok(Command::Ps));+ assert_eq!(+ parse(&args(&[+ "peer-account",+ "--peer-port",+ "5000",+ "--own-port",+ "18473",+ "--daemon-pid",+ "42"+ ])),+ Ok(Command::PeerAccount {+ peer_port: 5000,+ own_port: 18473,+ daemon_pid: 42+ })+ );+ assert!(parse(&args(&["peer-account", "--peer-port", "5000"])).is_err());+ assert!(+ parse(&args(&[+ "peer-account",+ "--peer-port",+ "0",+ "--own-port",+ "1",+ "--daemon-pid",+ "1"+ ]))+ .is_err()+ );+ assert!(+ parse(&args(&[+ "peer-account",+ "--peer-port",+ "x",+ "--own-port",+ "1",+ "--daemon-pid",+ "1"+ ]))+ .is_err()+ );+ assert!(+ parse(&args(&[+ "peer-account",+ "--peer-port",+ "70000",+ "--own-port",+ "1",+ "--daemon-pid",+ "1"+ ]))+ .is_err()+ );assert_eq!(parse(&args(&["serve", "--home", "/h"])),Ok(Command::Serve {diff --git a/pty/src/lib.rs b/pty/src/lib.rsindex a8b307094..4fcaf79b4 100644--- a/pty/src/lib.rs+++ b/pty/src/lib.rs@@ -13,6 +13,7 @@ pub mod logfile;pub mod pane;pub mod pane_ids;pub mod panes;+pub mod peer;pub mod protocol;pub mod ps;pub mod server;diff --git a/pty/src/peer.rs b/pty/src/peer.rsnew file mode 100644index 000000000..9c56cb277--- /dev/null+++ b/pty/src/peer.rs@@ -0,0 +1,631 @@+//! Who is calling over loopback, for the daemon's check (`cli/src/lib/peerAccount.ts`).+//!+//! `harness-pty peer-account` answers whether the client end of 127.0.0.1:peer -> 127.0.0.1:own+//! belongs to a process of this account. Any account can bind a free loopback port and hear a+//! secret, so a secret heard must open nothing (audit W1, docs/security/2026-11-02-team-account-windows.md).+//! Windows PowerShell took 20 to 25 s for this on the ARM runner; this takes milliseconds.+//!+//! [`verdict`] holds every rule, so they run in every platform's tests. The Windows layer only+//! gathers the facts. Anything that cannot be read is a no: this guards a credential.++/// `FILETIME` units in one second.+pub const TICKS_PER_SECOND: u64 = 10_000_000;++/// What the daemon starts for a lookup: this binary, the fallback PowerShell, and their console hosts.+const LOOKUP_PROCESSES: [&str; 3] = ["harness-pty.exe", "powershell.exe", "conhost.exe"];++/// How far ahead of the clock a connection's time may read, for a clock stepped back just after.+const CLOCK_SLACK: u64 = 5 * TICKS_PER_SECOND;++/// One row of the TCP table the connection's pair of ports matched.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct Row {+ pub pid: u32,+ /// When the connection was made, as a `FILETIME`; 0 when Windows gave none.+ pub created: u64,+}++/// What Windows says of the process that owns a row.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct Process {+ /// Its creation `FILETIME`; 0 when Windows gave none.+ pub started: u64,+ pub parent: Option<u32>,+ /// Its exe file name.+ pub name: String,+ /// Whether its token's user is this process's: `None` when Windows will not give it.+ pub this_account: Option<bool>,+}++#[derive(Debug, Clone, PartialEq, Eq)]+pub enum Verdict {+ Yes,+ No(String),+}++/// The answer for [rows], those of the exact pair; `process` reads one by id, `None` when it is+/// gone or cannot be opened. [lookup] is this process's id, [daemon] the daemon's, and [now] the+/// clock as a `FILETIME`.+pub fn verdict(+ rows: &[Row],+ lookup: u32,+ daemon: u32,+ now: u64,+ mut process: impl FnMut(u32) -> Option<Process>,+) -> Verdict {+ let no = |why: String| Verdict::No(why);+ let [row] = rows else {+ return no(format!("{} connections match", rows.len()));+ };+ // The lookup's own process is never the caller: it could have taken a freed id.+ if row.pid == lookup {+ return no("the lookup itself owns it".into());+ }+ let Some(caller) = process(row.pid) else {+ return no("its process is gone".into());+ };+ if row.created == 0 || caller.started == 0 {+ return no("Windows gave no start time".into());+ }+ // The rule below trusts this time. One that read late, not being the clock we think it is,+ // would turn the rule off without a sign, so it is no too: wrong either way, it fails closed.+ if row.created > now.saturating_add(CLOCK_SLACK) {+ return no(format!(+ "Windows gave a connection time later than the clock ({} > {now})",+ row.created+ ));+ }+ if caller.name.is_empty() {+ return no("Windows will not give its name".into());+ }+ if caller.parent.is_none() {+ return no("Windows will not give its parent".into());+ }+ // Windows keeps naming a socket's creator after it exits when a child inherited the handle,+ // and the id may have gone to another process since: only one that started before the+ // connection was made can be its owner. A connection time to the second alone is compared to+ // the second, so a fresh process that connected at once still reads as yes; a finer one closes+ // a reuse within that second too.+ let started = if row.created % TICKS_PER_SECOND == 0 {+ caller.started - caller.started % TICKS_PER_SECOND+ } else {+ caller.started+ };+ if started > row.created {+ return no(format!(+ "its process started after its connection ({} > {})",+ caller.started, row.created+ ));+ }+ // Nor another lookup running at once, or its console host, started in the connection's second:+ // it could have taken the freed id, and nothing of the kind the daemon starts calls its port+ // with a secret. The rule of W1's PowerShell query, word for word, so both give the same answers.+ if LOOKUP_PROCESSES+ .iter()+ .any(|name| caller.name.eq_ignore_ascii_case(name))+ && (caller.parent == Some(daemon)+ || caller+ .parent+ .and_then(&mut process)+ .is_some_and(|parent| parent.parent == Some(daemon)))+ {+ return no(format!("a {} the daemon started owns it", caller.name));+ }+ match caller.this_account {+ Some(true) => Verdict::Yes,+ Some(false) => no("its owner is another account".into()),+ None => no("Windows will not give its owner".into()),+ }+}++/// The clock as a `FILETIME`: 100 ns units since 1601.+pub fn filetime_now() -> u64 {+ const UNIX_EPOCH_SECONDS: u64 = 11_644_473_600;+ let since = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .unwrap_or_default();+ (UNIX_EPOCH_SECONDS + since.as_secs()) * TICKS_PER_SECOND+ + u64::from(since.subsec_nanos()) / 100+}++#[cfg(windows)]+pub use windows::ask;++#[cfg(windows)]+mod windows {+ use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};++ use windows_sys::Win32::Foundation::{ERROR_INSUFFICIENT_BUFFER, HANDLE, NO_ERROR};+ use windows_sys::Win32::NetworkManagement::IpHelper::{+ GetExtendedTcpTable, MIB_TCP_STATE_ESTAB, MIB_TCPROW_OWNER_MODULE,+ TCP_TABLE_OWNER_MODULE_CONNECTIONS,+ };+ use windows_sys::Win32::Networking::WinSock::AF_INET;+ use windows_sys::Win32::Security::{+ EqualSid, GetTokenInformation, TOKEN_QUERY, TOKEN_USER, TokenUser,+ };+ use windows_sys::Win32::System::Threading::{+ GetCurrentProcess, OpenProcessToken, PROCESS_NAME_WIN32, QueryFullProcessImageNameW,+ };++ use super::{Process, Row, Verdict, filetime_now, verdict};++ /// 127.0.0.1 as the table stores it: network order in memory.+ const LOOPBACK: u32 = u32::from_ne_bytes([127, 0, 0, 1]);++ /// The answer for the client end of 127.0.0.1:[peer] -> 127.0.0.1:[own].+ pub fn ask(peer: u16, own: u16, daemon: u32) -> Verdict {+ let rows = match rows(peer, own) {+ Ok(rows) => rows,+ Err(code) => return Verdict::No(format!("Windows gave no connections ({code})")),+ };+ let Some(this) = user_of(unsafe { GetCurrentProcess() }) else {+ return Verdict::No("Windows will not give this account".into());+ };+ verdict(&rows, std::process::id(), daemon, filetime_now(), |pid| {+ read(pid, &this)+ })+ }++ /// The established rows whose client end is [peer] and server end [own], both on 127.0.0.1.+ /// Windows lists a connection once from each end, so the pair of ports in this order is one.+ pub(super) fn rows(peer: u16, own: u16) -> Result<Vec<Row>, u32> {+ // `u64` words, so the table's 64-bit timestamps are aligned.+ let mut words = vec![0u64; 4096];+ loop {+ let mut size = (words.len() * 8) as u32;+ let code = unsafe {+ GetExtendedTcpTable(+ words.as_mut_ptr().cast(),+ &mut size,+ 0,+ AF_INET as u32,+ TCP_TABLE_OWNER_MODULE_CONNECTIONS,+ 0,+ )+ };+ if code == ERROR_INSUFFICIENT_BUFFER {+ words.resize((size as usize).div_ceil(8) + 512, 0);+ continue;+ }+ if code != NO_ERROR {+ return Err(code);+ }+ break;+ }+ // MIB_TCPTABLE_OWNER_MODULE: a count, then rows at the rows' own alignment.+ let count = unsafe { *words.as_ptr().cast::<u32>() } as usize;+ let first = unsafe {+ words+ .as_ptr()+ .cast::<u8>()+ .add(std::mem::align_of::<MIB_TCPROW_OWNER_MODULE>())+ }+ .cast::<MIB_TCPROW_OWNER_MODULE>();+ let table = unsafe { std::slice::from_raw_parts(first, count) };+ let port = |value: u32| u16::from_be(value as u16);+ Ok(table+ .iter()+ .filter(|r| {+ r.dwState == MIB_TCP_STATE_ESTAB as u32+ && r.dwLocalAddr == LOOPBACK+ && r.dwRemoteAddr == LOOPBACK+ && port(r.dwLocalPort) == peer+ && port(r.dwRemotePort) == own+ })+ .map(|r| Row {+ pid: r.dwOwningPid,+ created: r.liCreateTimestamp as u64,+ })+ .collect())+ }++ /// What Windows says of [pid]; `None` when it has exited or cannot be opened.+ fn read(pid: u32, this: &[u8]) -> Option<Process> {+ let (process, synchronize) = crate::ps::open_process(pid)?;+ let handle = process.as_raw_handle() as HANDLE;+ if crate::ps::process_exited(handle, synchronize) {+ return None;+ }+ Some(Process {+ started: crate::ps::process_started(handle).unwrap_or(0),+ parent: crate::ps::process_parent(handle),+ name: file_name(handle),+ this_account: user_of(handle).map(|user| same_sid(&user, this)),+ })+ }++ fn file_name(process: HANDLE) -> String {+ let mut path = vec![0u16; 1024];+ let mut size = path.len() as u32;+ let ok = unsafe {+ QueryFullProcessImageNameW(process, PROCESS_NAME_WIN32, path.as_mut_ptr(), &mut size)+ };+ if ok == 0 {+ return String::new();+ }+ let path = String::from_utf16_lossy(&path[..size as usize]);+ path.rsplit('\\').next().unwrap_or_default().to_string()+ }++ /// The `TOKEN_USER` of [process]'s token, in `u64` words as bytes; `None` when Windows will not+ /// give it, as for another account's process.+ fn user_of(process: HANDLE) -> Option<Vec<u8>> {+ let mut token: HANDLE = std::ptr::null_mut();+ if unsafe { OpenProcessToken(process, TOKEN_QUERY, &mut token) } == 0 {+ return None;+ }+ let token = unsafe { OwnedHandle::from_raw_handle(token as _) };+ let mut size = 0u32;+ unsafe {+ GetTokenInformation(+ token.as_raw_handle() as HANDLE,+ TokenUser,+ std::ptr::null_mut(),+ 0,+ &mut size,+ )+ };+ if size == 0 {+ return None;+ }+ let mut words = vec![0u64; (size as usize).div_ceil(8)];+ let ok = unsafe {+ GetTokenInformation(+ token.as_raw_handle() as HANDLE,+ TokenUser,+ words.as_mut_ptr().cast(),+ size,+ &mut size,+ )+ };+ if ok == 0 {+ return None;+ }+ let bytes =+ unsafe { std::slice::from_raw_parts(words.as_ptr().cast::<u8>(), words.len() * 8) };+ Some(sid_bytes(bytes))+ }++ /// The SID's own bytes out of a `TOKEN_USER` buffer.+ fn sid_bytes(buffer: &[u8]) -> Vec<u8> {+ let user = unsafe { &*buffer.as_ptr().cast::<TOKEN_USER>() };+ let sid = user.User.Sid.cast::<u8>();+ // A SID: revision, sub-authority count, 6 bytes of authority, then 4 bytes per sub-authority.+ let count = unsafe { *sid.add(1) } as usize;+ unsafe { std::slice::from_raw_parts(sid, 8 + count * 4) }.to_vec()+ }++ fn same_sid(a: &[u8], b: &[u8]) -> bool {+ let (mut a, mut b) = (aligned(a), aligned(b));+ unsafe { EqualSid(a.as_mut_ptr().cast(), b.as_mut_ptr().cast()) != 0 }+ }++ fn aligned(bytes: &[u8]) -> Vec<u32> {+ let mut words = vec![0u32; bytes.len().div_ceil(4)];+ unsafe {+ std::ptr::copy_nonoverlapping(bytes.as_ptr(), words.as_mut_ptr().cast(), bytes.len())+ };+ words+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ const T: u64 = 134_000_000_000_000_000;+ const LOOKUP: u32 = 50;+ const DAEMON: u32 = 60;+ const CALLER: u32 = 70;+ const NOW: u64 = T + 60 * TICKS_PER_SECOND;++ fn mine() -> Process {+ Process {+ started: T - 5 * TICKS_PER_SECOND,+ parent: Some(1),+ name: "node.exe".into(),+ this_account: Some(true),+ }+ }+ fn row(created: u64) -> Row {+ Row {+ pid: CALLER,+ created,+ }+ }+ fn asked(rows: &[Row], process: Option<Process>) -> Verdict {+ verdict(rows, LOOKUP, DAEMON, NOW, |pid| {+ let parent = process.as_ref().and_then(|p| p.parent);+ match pid {+ CALLER => process.clone(),+ // The parent of the process asked of, for the console-host rule.+ pid if Some(pid) == parent => Some(Process {+ parent: Some(DAEMON),+ name: "harness-pty.exe".into(),+ ..mine()+ }),+ _ => None,+ }+ })+ }+ fn is_no(answer: Verdict, why: &str) {+ match answer {+ Verdict::No(said) => assert!(said.contains(why), "said {said:?}, wanted {why:?}"),+ Verdict::Yes => panic!("said yes, wanted no: {why}"),+ }+ }++ #[test]+ fn a_process_of_this_account_that_started_first_is_yes() {+ assert_eq!(asked(&[row(T)], Some(mine())), Verdict::Yes);+ }++ #[test]+ fn no_row_or_more_than_one_cannot_be_told_apart() {+ is_no(asked(&[], Some(mine())), "0 connections");+ is_no(asked(&[row(T), row(T)], Some(mine())), "2 connections");+ }++ #[test]+ fn another_account_or_an_owner_windows_will_not_give_is_no() {+ is_no(+ asked(+ &[row(T)],+ Some(Process {+ this_account: Some(false),+ ..mine()+ }),+ ),+ "another account",+ );+ is_no(+ asked(+ &[row(T)],+ Some(Process {+ this_account: None,+ ..mine()+ }),+ ),+ "will not give",+ );+ }++ #[test]+ fn a_process_gone_is_no() {+ is_no(asked(&[row(T)], None), "gone");+ }++ #[test]+ fn the_lookup_itself_is_never_the_caller() {+ is_no(+ verdict(+ &[Row {+ pid: LOOKUP,+ created: T,+ }],+ LOOKUP,+ DAEMON,+ NOW,+ |_| Some(mine()),+ ),+ "the lookup itself",+ );+ }++ #[test]+ fn a_process_started_after_its_connection_has_taken_a_freed_id() {+ is_no(+ asked(+ &[row(T)],+ Some(Process {+ started: T + TICKS_PER_SECOND,+ ..mine()+ }),+ ),+ "started after",+ );+ }++ #[test]+ fn a_time_windows_did_not_give_is_no() {+ is_no(asked(&[row(0)], Some(mine())), "no start time");+ is_no(+ asked(+ &[row(T)],+ Some(Process {+ started: 0,+ ..mine()+ }),+ ),+ "no start time",+ );+ }++ // Windows may give the connection's time to the second alone, as Get-NetTCPConnection did on+ // the runs of #2605: then a fresh process that connected at once must still read as yes.+ #[test]+ fn a_connection_time_to_the_second_compares_to_the_second() {+ let second = T - T % TICKS_PER_SECOND;+ assert_eq!(+ asked(+ &[row(second)],+ Some(Process {+ started: second + 5,+ ..mine()+ })+ ),+ Verdict::Yes+ );+ is_no(+ asked(+ &[row(second)],+ Some(Process {+ started: second + TICKS_PER_SECOND,+ ..mine()+ }),+ ),+ "started after",+ );+ }++ // A connection time finer than a second closes W1's gap: no reuse within its own second.+ #[test]+ fn a_finer_connection_time_compares_exactly() {+ let fine = T - T % TICKS_PER_SECOND + 7;+ assert_eq!(+ asked(+ &[row(fine)],+ Some(Process {+ started: fine,+ ..mine()+ })+ ),+ Verdict::Yes+ );+ is_no(+ asked(+ &[row(fine)],+ Some(Process {+ started: fine + 1,+ ..mine()+ }),+ ),+ "started after",+ );+ }++ // W1's rule, whichever way the lookups run: the daemon's PowerShell, this binary, or the console+ // host of either, as its child or its child's child (Remy's review of #2605). While a fallback+ // PowerShell runs, a helper that just refused the command is alive too.+ #[test]+ fn what_the_daemon_starts_for_lookups_may_have_taken_a_freed_id() {+ for name in [+ "harness-pty.exe",+ "powershell.exe",+ "conhost.exe",+ "HARNESS-PTY.EXE",+ "PowerShell.EXE",+ ] {+ for parent in [DAEMON, 80] {+ let process = Process {+ parent: Some(parent),+ name: name.into(),+ ..mine()+ };+ is_no(asked(&[row(T)], Some(process)), "the daemon started");+ }+ }+ }++ #[test]+ fn a_powershell_the_daemon_did_not_start_is_yes() {+ let shell = Process {+ parent: Some(80),+ name: "powershell.exe".into(),+ ..mine()+ };+ let others = |pid| match pid {+ CALLER => Some(shell.clone()),+ 80 => Some(Process {+ parent: Some(2),+ name: "explorer.exe".into(),+ ..mine()+ }),+ _ => None,+ };+ assert_eq!(+ verdict(&[row(T)], LOOKUP, DAEMON, NOW, others),+ Verdict::Yes+ );+ // Its parent gone, as for a shell whose launcher exited: nothing ties it to the daemon.+ let orphan = |pid| (pid == CALLER).then(|| shell.clone());+ assert_eq!(+ verdict(&[row(T)], LOOKUP, DAEMON, NOW, orphan),+ Verdict::Yes+ );+ }++ // The recycled-id rule trusts the connection's time. One that read late, not being the clock+ // we think it is, would turn the rule off without a sign (Orion's review of #2647): every+ // process started before it, a reused id's too, would pass.+ #[test]+ fn a_connection_time_later_than_the_clock_is_no() {+ for created in [+ NOW + CLOCK_SLACK + 1,+ NOW + 3600 * TICKS_PER_SECOND,+ u64::MAX,+ ] {+ is_no(asked(&[row(created)], Some(mine())), "later than the clock");+ }+ assert_eq!(asked(&[row(NOW + CLOCK_SLACK)], Some(mine())), Verdict::Yes);+ }++ // Anything Windows would not say is a no, as this file's header promises (both reviews of #2647).+ #[test]+ fn a_name_or_parent_windows_would_not_give_is_no() {+ let nameless = Process {+ name: String::new(),+ ..mine()+ };+ is_no(asked(&[row(T)], Some(nameless)), "will not give its name");+ let parentless = Process {+ parent: None,+ ..mine()+ };+ is_no(+ asked(&[row(T)], Some(parentless)),+ "will not give its parent",+ );+ }++ #[test]+ fn the_clock_is_a_filetime() {+ // 2020-01-01 as a FILETIME: the clock is later, and not by centuries.+ const Y2020: u64 = 132_223_104_000_000_000;+ let now = filetime_now();+ assert!(now > Y2020 && now < Y2020 * 2, "{now}");+ }++ // The real run's proof that the rule above has a real clock to compare (Orion's review of+ // #2647): the table's time for a connection just made is this clock, near now. The line goes+ // to the run's log, past the test harness, to show what precision Windows gives.+ #[cfg(windows)]+ #[test]+ fn windows_gives_a_connection_time_on_this_clock() {+ use std::io::Write;+ let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();+ let own = listener.local_addr().unwrap().port();+ let client = std::net::TcpStream::connect(("127.0.0.1", own)).unwrap();+ let _accepted = listener.accept().unwrap();+ let peer = client.local_addr().unwrap().port();+ let rows = super::windows::rows(peer, own).unwrap();+ assert_eq!(rows.len(), 1, "{rows:?}");+ let (created, now) = (rows[0].created, filetime_now());+ let _ = writeln!(+ std::io::stderr(),+ "[peer] connection time {created}, clock {now}, finer than a second: {}",+ created % TICKS_PER_SECOND != 0+ );+ assert!(+ created.abs_diff(now) < 10 * TICKS_PER_SECOND,+ "{created} against {now}"+ );+ assert_eq!(rows[0].pid, std::process::id());+ }++ #[test]+ fn a_program_of_this_account_the_daemon_started_is_still_yes() {+ assert_eq!(+ asked(+ &[row(T)],+ Some(Process {+ parent: Some(DAEMON),+ name: "node.exe".into(),+ ..mine()+ })+ ),+ Verdict::Yes+ );+ }+}diff --git a/pty/src/ps.rs b/pty/src/ps.rsindex 021234698..5b6932950 100644--- a/pty/src/ps.rs+++ b/pty/src/ps.rs@@ -37,6 +37,11 @@ pub struct ProcessRowJson {#[cfg(windows)]pub use windows::snapshot;+#[cfg(windows)]+pub(crate) use windows::{+ creation_time as process_started, exited as process_exited, open as open_process,+ parent as process_parent,+};#[cfg(windows)]mod windows {@@ -210,7 +215,7 @@ mod windows {/// The process opened for queries, and whether the handle can be waited on (`SYNCHRONIZE`,/// which a process may refuse while it still allows limited queries).- fn open(pid: u32) -> Option<(OwnedHandle, bool)> {+ pub(crate) fn open(pid: u32) -> Option<(OwnedHandle, bool)> {[(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_SYNCHRONIZE,@@ -232,7 +237,7 @@ mod windows {/// Whether the process has exited: its handle is signaled; without `SYNCHRONIZE`, it has an/// exit code (a live process reports `STILL_ACTIVE`).- fn exited(h: HANDLE, synchronize: bool) -> bool {+ pub(crate) fn exited(h: HANDLE, synchronize: bool) -> bool {if synchronize {return unsafe { WaitForSingleObject(h, 0) } == WAIT_OBJECT_0;}@@ -241,7 +246,7 @@ mod windows {}/// The parent the kernel recorded for the process.- fn parent(h: HANDLE) -> Option<u32> {+ pub(crate) fn parent(h: HANDLE) -> Option<u32> {let mut info: PROCESS_BASIC_INFORMATION = unsafe { std::mem::zeroed() };let size = std::mem::size_of::<PROCESS_BASIC_INFORMATION>() as u32;let status = unsafe {@@ -257,7 +262,7 @@ mod windows {}/// The creation `FILETIME` as one number.- fn creation_time(h: HANDLE) -> Option<u64> {+ pub(crate) fn creation_time(h: HANDLE) -> Option<u64> {let zero = FILETIME {dwLowDateTime: 0,dwHighDateTime: 0,