Skip to content

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.

Batch M: Windows Git Bash follow-ups (#2069), machine removal cleanup (#2070), tag list digest (#2071) (#2072)

0xken1593dc2d596479
diff --git a/backend/integration/superTabRead.test.ts b/backend/integration/superTabRead.test.ts
index c81507ee1..030f6cc69 100644
--- a/backend/integration/superTabRead.test.ts
+++ b/backend/integration/superTabRead.test.ts
@@ -794,6 +794,35 @@ describe('decisions asked of one person (v2)', () => {
expect((await get(`/api/super-tabs/${s.tabId}/parts`, owner)).body.data.derived.decisionsDigest).toBe(left.decisionsDigest)
})
+ it('marks which tags wait, so an answer and a new tag read again though the count stays, an unrelated post not', async () => {
+ const s = await scene()
+ clock = Date.now()
+ const tag = async (conversationId: string) => expect((await post(s.tabId, { type: 'message.send', conversationId, text: 'Please look.', replyTo: null,
+ recipientIds: [], mentions: [peer.sub] }, owner, s.lead)).status).toBe(200)
+ const derived = async (as: AuthUser = peer) => (await get(`/api/super-tabs/${s.tabId}/parts`, as)).body.data
+ await tag(s.web)
+ const before = await derived()
+ expect(before.derived).toMatchObject({ mentionsWaiting: 1, mentionsDigest: expect.any(String) })
+ const [mention] = (await get(`/api/super-tabs/${s.tabId}/mentions?state=waiting`, peer)).body.data.mentions as Array<{ id: string }>
+ expect(before.derived.mentionsDigest).not.toContain(mention!.id)
+ // Someone else's post changes the Swarm and no tag.
+ expect((await post(s.tabId, { type: 'message.send', conversationId: s.web, text: 'Unrelated.', replyTo: null, recipientIds: [] })).status).toBe(200)
+ const changes = (await get(`/api/super-tabs/${s.tabId}/changes?cursor=${before.cursor}`, peer)).body.data
+ expect(changes.derived).toMatchObject({ mentionsWaiting: 1, mentionsDigest: before.derived.mentionsDigest })
+ // Answered by the person's reply, and tagged in another conversation: still one waiting, another one.
+ expect((await post(s.tabId, { type: 'message.send', conversationId: s.web, text: 'Looked.', replyTo: null, recipientIds: [] }, peer)).status).toBe(200)
+ await tag(s.privateId)
+ const replaced = (await derived()).derived
+ expect(replaced.mentionsWaiting).toBe(1)
+ expect(replaced.mentionsDigest).not.toBe(before.derived.mentionsDigest)
+ // 30 days on it expires; with none left it is the same as for anyone with none.
+ clock = Date.now() + 30 * DAY + 1000
+ const expired = (await derived()).derived
+ expect(expired.mentionsWaiting).toBe(0)
+ expect(expired.mentionsDigest).not.toBe(replaced.mentionsDigest)
+ expect((await derived(owner)).derived.mentionsDigest).toBe(expired.mentionsDigest)
+ })
+
it('counts every open decision the reader can open, past 200, reading only which ones and where', async () => {
const s = await scene()
clock = Date.now()
diff --git a/backend/src/superTabs/mentions.ts b/backend/src/superTabs/mentions.ts
index 20c56fae0..3ef56b3b7 100644
--- a/backend/src/superTabs/mentions.ts
+++ b/backend/src/superTabs/mentions.ts
@@ -1,6 +1,7 @@
/** A Lead tags people in its message; each tag waits in the person's Swarm-wide list until they reply in that
* conversation or mark it done. Tags live in SuperTabMention, never in the tab state, so an older backend still
* reads and writes a tab that has them. Called inside SuperTabStore's transactions. */
+import { createHash } from 'node:crypto'
import type { Prisma, PrismaClient } from '@prisma/client'
import { ensureIndex } from '../lib/db/indexes.js'
import { fail, isLeads, person, stableId, type Actor, type Conversation, type MessageDraft, type SuperTabState } from './model.js'
@@ -92,7 +93,16 @@ async function readable(tx: Tx, tabId: string, state: SuperTabState, actor: Extr
}
export async function mentionsWaiting(tx: Tx, tabId: string, state: SuperTabState, actor: Extract<Actor, { kind: 'person' }>, now: number): Promise<number> {
- return (await readable(tx, tabId, state, actor, now, true)).length
+ return (await waitingMentions(tx, tabId, state, actor, now)).count
+}
+
+/** A person's waiting tags: how many, and a digest of which, as decisions.ts decisionsWaiting. A client reads its lists again
+ * when either moves (a tag made, answered or expired, though the count may stay), not on every change of the Swarm. Only
+ * the person's own tags they can still read go into it, and only their ids. */
+export async function waitingMentions(tx: Tx, tabId: string, state: SuperTabState, actor: Extract<Actor, { kind: 'person' }>,
+ now: number): Promise<{ count: number; digest: string }> {
+ const ids = (await readable(tx, tabId, state, actor, now, true)).map(({ row }) => row.id).sort()
+ return { count: ids.length, digest: createHash('sha256').update(ids.join('\n')).digest('base64url').slice(0, 16) }
}
export async function listMentions(tx: Tx, tabId: string, state: SuperTabState, actor: Extract<Actor, { kind: 'person' }>, now: number, open: boolean) {
diff --git a/backend/src/superTabs/read/contract.ts b/backend/src/superTabs/read/contract.ts
index 9e9568a5c..813baaa16 100644
--- a/backend/src/superTabs/read/contract.ts
+++ b/backend/src/superTabs/read/contract.ts
@@ -63,6 +63,8 @@ export interface Derived {
otherProjectsWork?: OtherProjectWorkView[]
/** A person only. */
mentionsWaiting?: number
+ /** A person only: a digest of which tags wait, moving when they change though the count may not. */
+ mentionsDigest?: string
/** Signed links to uploaded avatars, by `person:<id>` and `agent:<id>` (avatarLinks.ts): fresh with every read, the same
* through each half hour, each opening for at most an hour. Absent when nobody visible has one. */
avatars?: Record<string, string>
diff --git a/backend/src/superTabs/read/service.ts b/backend/src/superTabs/read/service.ts
index a1e38a9fd..b256174a0 100644
--- a/backend/src/superTabs/read/service.ts
+++ b/backend/src/superTabs/read/service.ts
@@ -6,7 +6,7 @@ import type { PrismaClient } from '@prisma/client'
import type { AuthUser } from '../../lib/ssoAuth.js'
import { isTeamSession, TEAM_SESSION_LIMITED, TEAM_SESSION_MESSAGE } from '../../lib/teamSession.js'
import { author, fail, isLeads, seenMachineIds, visibleState, type Actor, type AgentStart, type SuperTabState } from '../model.js'
-import { mentionsWaiting } from '../mentions.js'
+import { waitingMentions } from '../mentions.js'
import { decisionsWaiting } from '../decisions.js'
import type { SuperTabStore } from '../store.js'
import { transaction } from '../store.js'
@@ -172,12 +172,13 @@ export class SwarmReads {
const actor = await this.store.actor(tx, user, state, asAgentId)
const [[teamMachines, otherWorkspaceMachines], counts, newest] = await Promise.all([
this.machineLookups(state),
- actor.kind === 'person' ? Promise.all([mentionsWaiting(tx, tabId, state, actor, t0), decisionsWaiting(tx, tabId, state, actor)]) : undefined,
+ actor.kind === 'person' ? Promise.all([waitingMentions(tx, tabId, state, actor, t0), decisionsWaiting(tx, tabId, state, actor)]) : undefined,
readLastChangedAt(tx, cached, tabId),
])
return { parts: loaded, actor, lastChangedAt: newest,
view: readerView(loaded, actor, t0, { teamMachines, otherWorkspaceMachines,
- ...(counts ? { mentionsWaiting: counts[0], decisionsWaiting: counts[1].count, decisionsDigest: counts[1].digest } : {}) }, state) }
+ ...(counts ? { mentionsWaiting: counts[0].count, mentionsDigest: counts[0].digest, decisionsWaiting: counts[1].count,
+ decisionsDigest: counts[1].digest } : {}) }, state) }
}
/** Which of the Swarm's machines are signed in as a team account, and which serve another Workspace's Swarm too (store.ts): the
diff --git a/backend/src/superTabs/read/views.test.ts b/backend/src/superTabs/read/views.test.ts
index e296c84b9..5779b8a4c 100644
--- a/backend/src/superTabs/read/views.test.ts
+++ b/backend/src/superTabs/read/views.test.ts
@@ -108,7 +108,8 @@ describe('reader view of the parts (design 4.1, 4.3)', () => {
const { s, parts } = scene()
expect(readerView(parts, agentActor(s.lead), NOW, { teamMachines: [] }).derived).toHaveProperty('otherProjectsWork', [])
expect(readerView(parts, agentActor(s.lead), NOW, { teamMachines: [] }).derived).not.toHaveProperty('mentionsWaiting')
- expect(readerView(parts, peer, NOW, { teamMachines: [], mentionsWaiting: 2 }).derived).toMatchObject({ mentionsWaiting: 2 })
+ expect(readerView(parts, peer, NOW, { teamMachines: [], mentionsWaiting: 2, mentionsDigest: 'd' }).derived).toMatchObject({ mentionsWaiting: 2, mentionsDigest: 'd' })
+ expect(readerView(parts, agentActor(s.lead), NOW, { teamMachines: [], mentionsWaiting: 2, mentionsDigest: 'd' }).derived).not.toHaveProperty('mentionsDigest')
expect(readerView(parts, peer, NOW, { teamMachines: [] }).derived).not.toHaveProperty('otherProjectsWork')
})
diff --git a/backend/src/superTabs/read/views.ts b/backend/src/superTabs/read/views.ts
index bf0959b37..e6abdc926 100644
--- a/backend/src/superTabs/read/views.ts
+++ b/backend/src/superTabs/read/views.ts
@@ -34,6 +34,8 @@ export interface ReadExtras {
otherWorkspaceMachines?: string[]
/** A person's open mentions, counted by the store. */
mentionsWaiting?: number
+ /** Which ones they are, as a digest (mentions.ts). */
+ mentionsDigest?: string
/** A person's open decisions (decisions.ts). */
decisionsWaiting?: number
/** Which ones they are, as a digest (decisions.ts). */
@@ -114,6 +116,7 @@ function derive(state: SuperTabState, visible: SuperTabState, actor: Actor, now:
machines: Object.fromEntries((visible.machines ?? []).map(machine)),
...(visible.otherProjectsWork ? { otherProjectsWork: visible.otherProjectsWork } : {}),
...(actor.kind === 'person' && extras.mentionsWaiting !== undefined ? { mentionsWaiting: extras.mentionsWaiting } : {}),
+ ...(actor.kind === 'person' && extras.mentionsDigest !== undefined ? { mentionsDigest: extras.mentionsDigest } : {}),
...(actor.kind === 'person' && extras.decisionsWaiting !== undefined ? { decisionsWaiting: extras.decisionsWaiting } : {}),
...(actor.kind === 'person' && extras.decisionsDigest !== undefined ? { decisionsDigest: extras.decisionsDigest } : {}),
...(teamAccountSwarm(state) ? {} : { teamAccounts: false as const }),
diff --git a/cli/src/dsh/materialize.spec.ts b/cli/src/dsh/materialize.spec.ts
index d23f3ec2a..bc5065f53 100644
--- a/cli/src/dsh/materialize.spec.ts
+++ b/cli/src/dsh/materialize.spec.ts
@@ -169,6 +169,19 @@ describe('materializeWorkspace, on harnesses other than the starter', () => {
expect((await materializeWorkspace(killed, workspace)).warnings).toEqual(['init exited SIGKILL'])
})
+ it('on Windows without Git Bash, says the workspace init could not start, not that a setup or check script needs it', async () => {
+ const platform = Object.getOwnPropertyDescriptor(process, 'platform')!
+ // A Mac or Linux PATH has no git.exe, and the Windows install folders are unset here: no Git Bash is found.
+ Object.defineProperty(process, 'platform', { ...platform, value: 'win32' })
+ try {
+ const result = await materializeWorkspace(install({ engine: 'codex', workspace: { marker: 'done', init: 'echo hi' } }), workspace)
+ expect(result.initLines).toEqual(["could not start: This harness's workspace init needs Git Bash on Windows, and it was not found. Install Git for Windows from https://git-scm.com/download/win (or run: winget install --id Git.Git -e), then try again."])
+ expect(result.warnings).toEqual(['init exited 127'])
+ } finally {
+ Object.defineProperty(process, 'platform', platform)
+ }
+ })
+
it('an init that is still running after five minutes is stopped and said to have timed out', async () => {
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
const slow = install({ engine: 'codex', workspace: { marker: 'done', init: 'sleep 30' } })
diff --git a/cli/src/dsh/materialize.ts b/cli/src/dsh/materialize.ts
index b8c281662..9af46306d 100644
--- a/cli/src/dsh/materialize.ts
+++ b/cli/src/dsh/materialize.ts
@@ -81,6 +81,7 @@ export async function materializeWorkspace(dsh: InstalledDsh, workspace: string,
env: { HARNESS_DSH: dsh.id, HARNESS_DSH_DIR: dsh.realDir, HARNESS_WORKSPACE: workspace, ...dshAccountEnv(account) },
onLine: (line) => result.initLines.push(line),
timeoutMs: 5 * 60_000,
+ purpose: 'workspace init',
})
if (init.code !== 0 || init.timedOut) {
result.warnings.push(`init exited ${init.timedOut ? 'by timeout' : init.code ?? init.signal}`)
diff --git a/cli/src/dsh/shell.spec.ts b/cli/src/dsh/shell.spec.ts
index 553b79750..1ba7143ee 100644
--- a/cli/src/dsh/shell.spec.ts
+++ b/cli/src/dsh/shell.spec.ts
@@ -7,14 +7,20 @@ import { spawnSync, type ChildProcess } from 'node:child_process'
// Two switches, both off by default: `sh` makes commands run under plain `/bin/sh -c` (as when no user
// shell is known) so output is exactly the command's; `fakeChild` hands runDshCommand a scripted child.
-const seams = vi.hoisted(() => ({ sh: false, fakeChild: null as null | (() => ChildProcess) }))
+// `started` collects the pid of each real process spawned, for a test that needs the shell's own.
+const seams = vi.hoisted(() => ({ sh: false, fakeChild: null as null | (() => ChildProcess), started: [] as Array<number | undefined> }))
vi.mock('../lib/engineLaunch.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('../lib/engineLaunch.js')>()
return { ...actual, interactiveEngineShell: (shell?: string) => (seams.sh ? null : actual.interactiveEngineShell(shell)) }
})
vi.mock('node:child_process', async (importOriginal) => {
const actual = await importOriginal<typeof import('node:child_process')>()
- const spawn = ((...args: Parameters<typeof actual.spawn>) => (seams.fakeChild ? seams.fakeChild() : actual.spawn(...args))) as typeof actual.spawn
+ const spawn = ((...args: Parameters<typeof actual.spawn>) => {
+ if (seams.fakeChild) return seams.fakeChild()
+ const child = actual.spawn(...args)
+ seams.started.push(child.pid)
+ return child
+ }) as typeof actual.spawn
return { ...actual, spawn, default: { ...actual, spawn } }
})
@@ -61,6 +67,13 @@ describe('dshShellArgv on Windows', () => {
.toThrow('This harness runs a setup or check script, which needs Git Bash on Windows, and it was not found. Install Git for Windows from https://git-scm.com/download/win (or run: winget install --id Git.Git -e), then try again.')
})
+ it('names what could not start when it is a viewer or a workspace init, which are not setup or check scripts', () => {
+ expect(() => dshShellArgv('./viewer.sh', { platform: 'win32', gitBash: () => null }, 'viewer'))
+ .toThrow("This harness's viewer needs Git Bash on Windows, and it was not found. Install Git for Windows")
+ expect(() => dshShellArgv('./init.sh', { platform: 'win32', gitBash: () => null }, 'workspace init'))
+ .toThrow("This harness's workspace init needs Git Bash on Windows, and it was not found. Install Git for Windows")
+ })
+
describe('the script it runs, in a real bash', () => {
let dir: string
beforeEach(() => { dir = realpathSync(mkdtempSync(join(tmpdir(), 'dsh-gitbash-'))) })
@@ -145,6 +158,15 @@ describe('isShellNoise', () => {
expect(isShellNoise(' ok bash: no job control in this shell')).toBe(false)
expect(isShellNoise('logout failed')).toBe(false)
})
+
+ it('keeps a line that names some other bash: only bash itself, or the paths it is installed at, says the notice', () => {
+ // Review of #2025: any `<path>/bash: no job control in this shell` was dropped, a script's own line included.
+ expect(isShellNoise('scripts/bash: no job control in this shell')).toBe(false)
+ expect(isShellNoise('./bash: no job control in this shell')).toBe(false)
+ expect(isShellNoise('/home/ana/project/bash: cannot set terminal process group (-1): Inappropriate ioctl for device')).toBe(false)
+ expect(isShellNoise('mybash: no job control in this shell')).toBe(false)
+ expect(isShellNoise('/bin/bash: cannot set terminal process group (4242): Inappropriate ioctl for device')).toBe(true)
+ })
})
describe('runDshCommand', () => {
@@ -192,6 +214,26 @@ describe('runDshCommand', () => {
expect(Date.now() - started).toBeLessThan(10_000)
})
+ it('on Windows, ends the whole tree of a Git Bash run that outlives its timeout, not the shell alone', async () => {
+ // Windows has no process groups: a signal to the group reached nothing, and the one to the shell ended Git Bash's
+ // launcher alone, leaving what the doctor started running (review of #2025). Here a real bash stands in for Git
+ // Bash, and the stand-in for the tree killer ends its group, as TerminateProcess down the tree does there.
+ const ended: number[] = []
+ const endTree = async (pid: number): Promise<void> => { ended.push(pid); process.kill(-pid, 'SIGKILL') }
+ // The shell's pid is taken from the spawn, not from what the shell prints: a login bash reads the host's profile
+ // first, and on the Linux runner that outlived the timeout, so the script never printed it (#2069 in CI). This
+ // home's profile is as slow, so every host takes the same path.
+ writeFileSync(join(dir, '.bash_profile'), 'sleep 5\n')
+ seams.started = []
+ const result = await runDshCommand('sleep 30 & wait', {
+ cwd: dir, timeoutMs: 300, env: { HOME: dir, PATH: '/usr/bin:/bin' },
+ host: { platform: 'win32', gitBash: () => '/bin/bash', endTree },
+ })
+ expect(result.timedOut).toBe(true)
+ expect(seams.started).toHaveLength(1)
+ expect(ended).toEqual(seams.started)
+ })
+
it('does not take the shell\'s report of the job its stop ended for the command\'s output', async () => {
// The shell outlives its job here on purpose: it ignores SIGTERM, so it is in `wait` when the job the stop
// ended dies, and reports it, as it did under load when the job's death reached it first.
diff --git a/cli/src/dsh/shell.ts b/cli/src/dsh/shell.ts
index b23af905f..088b3903b 100644
--- a/cli/src/dsh/shell.ts
+++ b/cli/src/dsh/shell.ts
@@ -10,6 +10,7 @@ import { harnessNodePrelude, interactiveEngineShell } from '../lib/engineLaunch.
import { managedNodePath } from '../lib/nodeRuntime.js'
import { shellSingleQuote } from '../lib/shellQuote.js'
import { findGitBash, INSTALL_GIT_FOR_WINDOWS } from '../lib/windows/gitBash.js'
+import { endGitBashTree, gitBashTreeDeps } from '../lib/windows/gitBashTree.js'
export interface DshCommandOptions {
cwd: string
@@ -19,8 +20,13 @@ export interface DshCommandOptions {
timeoutMs?: number
/** The platform and Git Bash to run under, when not this computer's: a test runs Windows' path on any computer. */
host?: DshShellHost
+ /** What the command is, as said when it cannot start: a setup or check script unless said otherwise. */
+ purpose?: DshCommandPurpose
}
+/** What a DSH's command is to a person: a setup or a doctor, the viewer, or the workspace's init. */
+export type DshCommandPurpose = 'setup or check script' | 'viewer' | 'workspace init'
+
export interface DshCommandResult {
code: number | null
signal: NodeJS.Signals | null
@@ -40,7 +46,12 @@ export function isShellNoise(line: string): boolean {
|| BASH_NO_TTY.test(line) || line === 'logout'
}
-const BASH_NO_TTY = /^(?:\S*\/)?bash: (?:cannot set terminal process group \(-?\d+\): Inappropriate ioctl for device|no job control in this shell)$/
+/**
+ * The notice word for word, said by bash under a name it has: an interactive bash says its own base name (`bash`, Git
+ * Bash's too), and the paths are the ones bash is installed at, `/usr/bin/bash` as Linux CI saw it. Not any
+ * `<path>/bash:`, which dropped a script's own line that only looked like the notice (review of #2025).
+ */
+const BASH_NO_TTY = /^(?:bash|\/bin\/bash|\/usr\/bin\/bash): (?:cannot set terminal process group \(-?\d+\): Inappropriate ioctl for device|no job control in this shell)$/
/**
* The shell's report of a job of its own that a signal ended (`/bin/sh: line 1: 31849 Terminated: 15 sleep 30`
@@ -65,8 +76,12 @@ export function isKilledJobReport(line: string): boolean {
* is sourced from `.bash_profile`. A setup or a doctor is one process, so the reason bash panes avoid
* `-l` (a PATH built in .bashrc compounding across subshells) does not apply here.
*/
-export function dshShellArgv(script: string, host: DshShellHost = {}): { path: string; args: string[]; env?: Record<string, string> } {
- if ((host.platform ?? process.platform) === 'win32') return gitBashArgv(script, (host.gitBash ?? findGitBash)())
+export function dshShellArgv(
+ script: string,
+ host: DshShellHost = {},
+ purpose: DshCommandPurpose = 'setup or check script',
+): { path: string; args: string[]; env?: Record<string, string> } {
+ if ((host.platform ?? process.platform) === 'win32') return gitBashArgv(script, (host.gitBash ?? findGitBash)(), purpose)
const shell = interactiveEngineShell()
const body = `${dshNodeFallback()}\n${script}`
if (shell) {
@@ -76,16 +91,27 @@ export function dshShellArgv(script: string, host: DshShellHost = {}): { path: s
return { path: '/bin/sh', args: ['-c', body] }
}
-/** The platform and the Git Bash lookup `dshShellArgv` uses; a test gives Windows' on any computer. */
+/** The platform, the Git Bash lookup `dshShellArgv` uses and how a stop ends a run there; a test gives Windows' on
+ * any computer. */
export interface DshShellHost {
platform?: NodeJS.Platform
gitBash?: () => string | null
+ /** Windows: end [pid] and everything it started (`endWindowsTree` by default). */
+ endTree?: (pid: number) => Promise<unknown>
}
/** What a person is told when a package's setup or doctor cannot run here: a Windows computer without Git Bash, as an
* install's refusal (before the download, or once fetched) or as a command's one line. */
export const DSH_SHELL_UNAVAILABLE = `This harness runs a setup or check script, which needs Git Bash on Windows, and it was not found. ${INSTALL_GIT_FOR_WINDOWS}`
+/** The same, said of the command that could not start. A viewer or a workspace init is not a setup or check script, and
+ * was called one (review of #2025). */
+export function dshShellUnavailable(purpose: DshCommandPurpose): string {
+ return purpose === 'setup or check script'
+ ? DSH_SHELL_UNAVAILABLE
+ : `This harness's ${purpose} needs Git Bash on Windows, and it was not found. ${INSTALL_GIT_FOR_WINDOWS}`
+}
+
/**
* Windows: Git Bash, as a login shell like the user's shell elsewhere, so its profile builds PATH (and puts cygpath
* on it). Thrown when there is none, and runDshCommand reports that as the command's one line: the doctor or setup
@@ -99,8 +125,8 @@ export const DSH_SHELL_UNAVAILABLE = `This harness runs a setup or check script,
* CHERE_INVOKING: Git for Windows' /etc/profile changes a login shell to $HOME unless it is set, and a setup or doctor
* runs in the harness's folder (review of #2025).
*/
-function gitBashArgv(script: string, bash: string | null): { path: string; args: string[]; env: Record<string, string> } {
- if (!bash) throw new Error(DSH_SHELL_UNAVAILABLE)
+function gitBashArgv(script: string, bash: string | null, purpose: DshCommandPurpose): { path: string; args: string[]; env: Record<string, string> } {
+ if (!bash) throw new Error(dshShellUnavailable(purpose))
const runtimeDir = shellSingleQuote(win32.dirname(managedNodePath()))
const body = [
'unalias node 2>/dev/null || true',
@@ -132,8 +158,11 @@ export function dshNodeFallback(): string {
return harnessNodePrelude(managedNodePath()).trimEnd()
}
-export function spawnDshCommand(script: string, opts: { cwd: string; env?: Record<string, string>; host?: DshShellHost }): ChildProcess {
- const { path, args, env } = dshShellArgv(script, opts.host)
+export function spawnDshCommand(
+ script: string,
+ opts: { cwd: string; env?: Record<string, string>; host?: DshShellHost; purpose?: DshCommandPurpose },
+): ChildProcess {
+ const { path, args, env } = dshShellArgv(script, opts.host, opts.purpose)
return spawn(path, args, {
cwd: opts.cwd,
env: { ...process.env, ...env, ...(opts.env ?? {}) },
@@ -151,7 +180,7 @@ export function runDshCommand(script: string, opts: DshCommandOptions): Promise<
let settled = false
let child: ChildProcess
try {
- child = spawnDshCommand(script, { cwd: opts.cwd, env: opts.env, host: opts.host })
+ child = spawnDshCommand(script, { cwd: opts.cwd, env: opts.env, host: opts.host, purpose: opts.purpose })
} catch (error) {
// spawn throws only Errors (an invalid argument: a NUL in the cwd, say).
const line = `could not start: ${(error as Error).message}`
@@ -186,7 +215,7 @@ export function runDshCommand(script: string, opts: DshCommandOptions): Promise<
const timer = opts.timeoutMs
? setTimeout(() => {
timedOut = true
- killProcessGroup(child)
+ killProcessGroup(child, undefined, opts.host)
}, opts.timeoutMs)
: null
timer?.unref?.()
@@ -200,19 +229,33 @@ export function runDshCommand(script: string, opts: DshCommandOptions): Promise<
})
}
-/** SIGTERM the child's whole group, then SIGKILL what is left a moment later. */
-export function killProcessGroup(child: ChildProcess, graceMs = 3_000): void {
+/** SIGTERM the child's whole group, then SIGKILL what is left a moment later; on Windows, end its tree. */
+export function killProcessGroup(child: ChildProcess, graceMs = 3_000, host: DshShellHost = {}): void {
const pid = child.pid
if (!pid) return
- const timer = killPidGroup(pid, graceMs)
+ const timer = killPidGroup(pid, graceMs, host)
child.once('exit', () => clearTimeout(timer))
}
+/** [pid], a Git Bash run, and everything it started: what Git Bash lists as its tree and the Windows tree under each,
+ * the furthest first (lib/windows/gitBashTree.ts). */
+function endWindowsTree(pid: number, gitBash: () => string | null): Promise<unknown> {
+ return endGitBashTree(pid, gitBashTreeDeps(gitBash()))
+}
+
/** The same, for a group this process did not spawn (a viewer left over from an earlier daemon —
* see viewerLedger.ts): nothing to wait on, so the SIGKILL follow-up fires unconditionally. */
-export function killPidGroup(pid: number, graceMs = 3_000): NodeJS.Timeout {
+export function killPidGroup(pid: number, graceMs = 3_000, host: DshShellHost = {}): NodeJS.Timeout {
// pid 1 would make `-pid` "every process in my session"; nothing this daemon reaps is init.
if (!Number.isInteger(pid) || pid <= 1) return setTimeout(() => {}, 0)
+ // Windows has no process groups, so `-pid` names nothing, and a signal is TerminateProcess of that one process: Git
+ // Bash's bin\bash.exe is a launcher, and ending it alone left the real bash, and what the setup or doctor started,
+ // running after a timeout (review of #2025). Its whole tree is ended instead, at once: nothing there gets a SIGTERM.
+ if ((host.platform ?? process.platform) === 'win32') {
+ const endTree = host.endTree ?? ((root: number) => endWindowsTree(root, host.gitBash ?? findGitBash))
+ void endTree(pid).catch(() => undefined)
+ return setTimeout(() => {}, 0)
+ }
const signalGroup = (signal: NodeJS.Signals): void => {
try { process.kill(-pid, signal) } catch { /* already gone */ }
try { process.kill(pid, signal) } catch { /* already gone */ }
diff --git a/cli/src/dsh/viewer.spec.ts b/cli/src/dsh/viewer.spec.ts
index 0b81c963a..a3a19964e 100644
--- a/cli/src/dsh/viewer.spec.ts
+++ b/cli/src/dsh/viewer.spec.ts
@@ -6,6 +6,7 @@ import { createServer, type Server } from 'node:net'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import type { InstalledDsh } from './installed.js'
+import { spawnDshCommand } from './shell.js'
import { DshViewerManager, buildViewerUrl, freeLoopbackPort, resolveViewer, waitForLoopbackPort } from './viewer.js'
// Off by default: set `net.fake` to hand the viewer a scripted server or socket instead of a real one.
@@ -17,6 +18,21 @@ vi.mock('node:net', async (importOriginal) => {
return { ...actual, createServer, connect, default: { ...actual, createServer, connect } }
})
+// Every artifact watcher the manager opens, and whether it was closed: what a viewer that failed must not keep.
+const watchers = vi.hoisted(() => [] as Array<{ closed: boolean }>)
+vi.mock('chokidar', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('chokidar')>()
+ const watch = ((...args: Parameters<typeof actual.watch>) => {
+ const watcher = actual.watch(...args)
+ const seen = { closed: false }
+ watchers.push(seen)
+ const close = watcher.close.bind(watcher)
+ watcher.close = () => { seen.closed = true; return close() }
+ return watcher
+ }) as typeof actual.watch
+ return { ...actual, watch, default: { ...actual.default, watch } }
+})
+
function fakeChild(): ChildProcess & { exitWith: (code: number) => void } {
const child = new EventEmitter() as ChildProcess & { exitWith: (code: number) => void }
Object.assign(child, {
@@ -452,6 +468,61 @@ describe('DshViewerManager, the rest of a viewer\'s life', () => {
expect(logs).toContain('[dsh] acme/thing viewer did not listen on 4790 within 60s')
})
+ describe('a viewer whose start failed or timed out keeps nothing running', () => {
+ const watched = () => dsh({ command: 'v', url: 'http://127.0.0.1:${port}/', artifactExtensions: ['.step'] })
+ let workspace: string
+ beforeEach(() => {
+ watchers.length = 0
+ workspace = join(root, 'ws')
+ mkdirSync(workspace)
+ })
+
+ it('on Windows without Git Bash: says the viewer could not start, closes its watch, and is not tried again on every attach', async () => {
+ let tries = 0
+ const noGitBash = ((script: string, opts: Parameters<typeof spawnDshCommand>[1]) => {
+ tries++
+ return spawnDshCommand(script, { ...opts, host: { platform: 'win32', gitBash: () => null } })
+ }) as typeof spawnDshCommand
+ const { logs, urls, manager } = setup({ spawn: noGitBash })
+ await manager.start('a1', watched(), workspace)
+ expect(logs).toContain("[dsh] acme/thing viewer could not start · This harness's viewer needs Git Bash on Windows, and it was not found. Install Git for Windows from https://git-scm.com/download/win (or run: winget install --id Git.Git -e), then try again.")
+ expect(watchers).toHaveLength(1)
+ expect(watchers[0].closed).toBe(true)
+ // The agents' scans attach every live agent again; the same viewer that just failed is not started each time.
+ await manager.start('a1', watched(), workspace)
+ expect(tries).toBe(1)
+ // A stop, as when the agent restarts, lets it try again.
+ await manager.stop('a1')
+ await manager.start('a1', watched(), workspace)
+ expect(tries).toBe(2)
+ expect(urls).toEqual([])
+ })
+
+ it('a viewer that never listens is stopped for good: not restarted, its watch closed', async () => {
+ const { spawned, logs, manager } = setup({ waitForPort: async () => false })
+ await manager.start('a1', watched(), workspace)
+ expect(logs).toContain('[dsh] acme/thing viewer did not listen on 4790 within 60s')
+ // The stop ends the child; its exit is not a crash to restart.
+ spawned[0].emit('exit', null, 'SIGTERM')
+ await settle(1_200)
+ expect(spawned).toHaveLength(1)
+ expect(logs.filter((line) => line.includes('restarting'))).toEqual([])
+ expect(watchers[0].closed).toBe(true)
+ await manager.start('a1', watched(), workspace)
+ expect(spawned).toHaveLength(1)
+ })
+
+ it('a viewer whose process could not be started, or that found no port, closes its watch', async () => {
+ const failing = setup({ waitForPort: async () => { failing.spawned[0].emit('error', new Error('spawn EACCES')); return false } })
+ await failing.manager.start('a1', watched(), workspace)
+ expect(failing.logs).toContain('[dsh] acme/thing viewer could not start · spawn EACCES')
+ expect(watchers[0].closed).toBe(true)
+ const noPort = setup({ freePort: async () => { throw new Error('EMFILE') } })
+ await noPort.manager.start('a2', watched(), workspace)
+ expect(watchers[1].closed).toBe(true)
+ })
+ })
+
it('follows the newest artifact as files change, in a workspace whose own path has an ignored name in it', async () => {
// `build` is a folder name the artifact scan skips INSIDE a workspace; a workspace that itself lives
// under a build/ folder used to have every change refused, so the pane never followed a new file.
diff --git a/cli/src/dsh/viewer.ts b/cli/src/dsh/viewer.ts
index e7f1710e2..c9bb04704 100644
--- a/cli/src/dsh/viewer.ts
+++ b/cli/src/dsh/viewer.ts
@@ -143,6 +143,11 @@ export function buildViewerUrl(template: string, port: number, artifact: string
return template.replace(/\$\{port\}/g, String(port)).replace(/\$\{artifact\}/g, encoded)
}
+/** One agent's viewer, as `start` tells a viewer already running (or one that failed) from another. */
+function viewerKey(dsh: InstalledDsh, viewer: ResolvedViewer, workspace: string): string {
+ return JSON.stringify([dsh.realDir, viewer.dir, workspace])
+}
+
export class DshViewerManager {
private readonly states = new Map<string, ViewerState>()
/**
@@ -151,6 +156,13 @@ export class DshViewerManager {
* kept here, a restored agent's pane opens on that artifact instead of on none.
*/
private readonly verdictArtifacts = new Map<string, string | null>()
+ /**
+ * The viewer each agent last failed to start (`viewerKey`): it could not be started, found no port, or never listened.
+ * Its watch and process are gone, but the agents' scans attach every live agent again, and starting the same viewer
+ * at each would log a failure, or wait out another minute for a port, over and over. A stop, or another viewer or
+ * workspace, forgets it (review of #2025: a failed viewer stayed running in the list, its watch open).
+ */
+ private readonly failed = new Map<string, string>()
constructor(private readonly deps: DshViewerDeps) {}
@@ -178,8 +190,11 @@ export class DshViewerManager {
return
}
const viewer = resolved.viewer
+ const key = viewerKey(dsh, viewer, workspace)
+ if (this.failed.get(agentId) === key) return
+ this.failed.delete(agentId)
const current = this.states.get(agentId)
- if (current && current.dsh.realDir === dsh.realDir && current.viewer.dir === viewer.dir && current.workspace === workspace) return
+ if (current && viewerKey(current.dsh, current.viewer, current.workspace) === key) return
// A restart keeps what the verdict last said; only stop() forgets it.
if (current) await this.halt(current)
const state: ViewerState = {
@@ -249,7 +264,7 @@ export class DshViewerManager {
try {
port = await (this.deps.freePort ?? freeLoopbackPort)()
} catch (error) {
- this.log(`[dsh] ${state.dsh.id} viewer: no free port · ${error instanceof Error ? error.message : error}`)
+ await this.fail(state, `[dsh] ${state.dsh.id} viewer: no free port · ${error instanceof Error ? error.message : error}`)
return
}
if (state.stopping) return
@@ -257,17 +272,25 @@ export class DshViewerManager {
// HARNESS_DSH/_DIR stay the harness's (the contract every DSH script relies on), HARNESS_VIEWER
// and HARNESS_VIEWER_DIR name the package. A harness's own viewer sees both pairs agree.
// Resolved inside the viewer's directory, as check.ts reads it: a bare `viewer.sh` is that file.
- const child = (this.deps.spawn ?? spawnDshCommand)(resolveDshCommand({ realDir: viewer.dir }, viewer.command), {
- cwd: viewer.dir,
- env: {
- HARNESS_DSH: state.dsh.id,
- HARNESS_DSH_DIR: state.dsh.realDir,
- HARNESS_VIEWER: viewer.id,
- HARNESS_VIEWER_DIR: viewer.dir,
- HARNESS_WORKSPACE: state.workspace,
- HARNESS_VIEWER_PORT: String(port),
- },
- })
+ let child: ChildProcess
+ try {
+ child = (this.deps.spawn ?? spawnDshCommand)(resolveDshCommand({ realDir: viewer.dir }, viewer.command), {
+ cwd: viewer.dir,
+ env: {
+ HARNESS_DSH: state.dsh.id,
+ HARNESS_DSH_DIR: state.dsh.realDir,
+ HARNESS_VIEWER: viewer.id,
+ HARNESS_VIEWER_DIR: viewer.dir,
+ HARNESS_WORKSPACE: state.workspace,
+ HARNESS_VIEWER_PORT: String(port),
+ },
+ purpose: 'viewer',
+ })
+ } catch (error) {
+ // Windows without Git Bash (shell.ts says what to install), or an argument spawn refuses.
+ await this.fail(state, `[dsh] ${state.dsh.id} viewer could not start · ${error instanceof Error ? error.message : error}`)
+ return
+ }
state.child = child
if (child.pid) this.deps.ledger?.add({ pid: child.pid, agentId: state.agentId, dshId: state.dsh.id, viewerDir: viewer.dir })
let logged = 0
@@ -279,7 +302,11 @@ export class DshViewerManager {
}
child.stdout?.on('data', onData)
child.stderr?.on('data', onData)
- child.on('error', (error) => this.log(`[dsh] ${state.dsh.id} viewer could not start · ${error.message}`))
+ child.on('error', (error) => {
+ const line = `[dsh] ${state.dsh.id} viewer could not start · ${error.message}`
+ if (state.child === child) void this.fail(state, line)
+ else this.log(line)
+ })
child.on('exit', (code, signal) => {
if (child.pid) this.deps.ledger?.remove(child.pid)
if (state.child !== child) return
@@ -301,8 +328,8 @@ export class DshViewerManager {
const up = await (this.deps.waitForPort ?? waitForLoopbackPort)(port, PORT_TIMEOUT_MS)
if (state.child !== child || state.stopping) return
if (!up) {
- this.log(`[dsh] ${state.dsh.id} viewer did not listen on ${port} within ${PORT_TIMEOUT_MS / 1000}s`)
- killProcessGroup(child)
+ // Stopped for good, not restarted: a viewer that did not listen in a minute would not in the next.
+ await this.fail(state, `[dsh] ${state.dsh.id} viewer did not listen on ${port} within ${PORT_TIMEOUT_MS / 1000}s`)
return
}
// Only now is there a URL to hand the pane. A pane sent to a port nobody serves yet shows a
@@ -312,9 +339,19 @@ export class DshViewerManager {
this.publish(state)
}
+ /** The viewer did not start: say why, stop what it left (its process, its watch), and remember it failed. */
+ private async fail(state: ViewerState, line: string): Promise<void> {
+ this.log(line)
+ // A stop, or a start of another viewer, has already replaced this one.
+ if (state.stopping || this.states.get(state.agentId) !== state) return
+ this.failed.set(state.agentId, viewerKey(state.dsh, state.viewer, state.workspace))
+ await this.halt(state)
+ }
+
/** The agent's viewer is done: stop it and forget what its verdict named. */
async stop(agentId: string): Promise<void> {
this.verdictArtifacts.delete(agentId)
+ this.failed.delete(agentId)
const state = this.states.get(agentId)
if (state) await this.halt(state)
}
diff --git a/cli/src/lib/windows/gitBash.spec.ts b/cli/src/lib/windows/gitBash.spec.ts
index 0d5c88533..7c4ce1e40 100644
--- a/cli/src/lib/windows/gitBash.spec.ts
+++ b/cli/src/lib/windows/gitBash.spec.ts
@@ -35,6 +35,24 @@ describe('findGitBash', () => {
expect(findGitBash({ env: { PATH: 'C:\\Windows\\System32;D:\\MinGit\\cmd' }, isFile: wsl })).toBeNull()
})
+ it('finds the git Scoop installed through the shim on PATH, whose own folder has no bash', () => {
+ // Scoop puts <root>\shims on PATH; git itself, bash with it, is <root>\apps\git\current.
+ const scoop = disk('C:\\Users\\ana\\scoop\\shims\\git.exe', 'C:\\Users\\ana\\scoop\\apps\\git\\current\\bin\\bash.exe')
+ expect(findGitBash({ env: { PATH: 'C:\\Windows\\System32;C:\\Users\\ana\\scoop\\shims' }, isFile: scoop }))
+ .toBe('C:\\Users\\ana\\scoop\\apps\\git\\current\\bin\\bash.exe')
+ })
+
+ it('looks where Scoop installs git when PATH has none yet: its own root, the home folder\'s, the global one', () => {
+ const custom = disk('D:\\scoop\\apps\\git\\current\\bin\\bash.exe')
+ expect(findGitBash({ env: { SCOOP: 'D:\\scoop' }, isFile: custom })).toBe('D:\\scoop\\apps\\git\\current\\bin\\bash.exe')
+ const home = disk('C:\\Users\\ana\\scoop\\apps\\git\\current\\bin\\bash.exe')
+ expect(findGitBash({ env: { USERPROFILE: 'C:\\Users\\ana' }, isFile: home })).toBe('C:\\Users\\ana\\scoop\\apps\\git\\current\\bin\\bash.exe')
+ const global = disk('E:\\apps-global\\apps\\git\\current\\bin\\bash.exe')
+ expect(findGitBash({ env: { SCOOP_GLOBAL: 'E:\\apps-global' }, isFile: global })).toBe('E:\\apps-global\\apps\\git\\current\\bin\\bash.exe')
+ const programData = disk('C:\\ProgramData\\scoop\\apps\\git\\current\\bin\\bash.exe')
+ expect(findGitBash({ env: { ProgramData: 'C:\\ProgramData' }, isFile: programData })).toBe('C:\\ProgramData\\scoop\\apps\\git\\current\\bin\\bash.exe')
+ })
+
it('is null on a computer with no Git for Windows at all', () => {
expect(findGitBash({ env: { PATH: 'C:\\Windows\\System32', ProgramFiles: 'C:\\Program Files' }, isFile: disk() })).toBeNull()
expect(findGitBash({ env: {}, isFile: disk() })).toBeNull()
diff --git a/cli/src/lib/windows/gitBash.ts b/cli/src/lib/windows/gitBash.ts
index 32afd9613..dcb7faffe 100644
--- a/cli/src/lib/windows/gitBash.ts
+++ b/cli/src/lib/windows/gitBash.ts
@@ -21,6 +21,9 @@ export interface GitBashSearch {
/** What a person is told to do when Git for Windows is missing. */
export const INSTALL_GIT_FOR_WINDOWS = 'Install Git for Windows from https://git-scm.com/download/win (or run: winget install --id Git.Git -e), then try again.'
+/** Git's bash under a Scoop root: Scoop keeps each app's current version at apps\<app>\current. */
+const SCOOP_GIT_BASH = win32.join('apps', 'git', 'current', 'bin', 'bash.exe')
+
export function findGitBash({ env = process.env, isFile = fileExists }: GitBashSearch = {}): string | null {
const candidates: string[] = []
// The git on PATH first, as the person's own terminal would run it. Git for Windows puts it in <root>\cmd (the
@@ -28,12 +31,19 @@ export function findGitBash({ env = process.env, isFile = fileExists }: GitBashS
for (const dir of (env.PATH ?? '').split(';').filter(Boolean)) {
if (!isFile(win32.join(dir, 'git.exe'))) continue
candidates.push(win32.resolve(dir, '..', 'bin', 'bash.exe'), win32.resolve(dir, '..', '..', 'bin', 'bash.exe'))
+ // Scoop's git.exe on PATH is a shim in <root>\shims; the git it starts, and its bash, are in <root>\apps\git\current.
+ candidates.push(win32.resolve(dir, '..', SCOOP_GIT_BASH))
}
// Then where its installer puts it: for everyone, or for this user alone (no administrator rights).
for (const root of [env.ProgramFiles, env.ProgramW6432, env['ProgramFiles(x86)']]) {
if (root) candidates.push(win32.join(root, 'Git', 'bin', 'bash.exe'))
}
if (env.LOCALAPPDATA) candidates.push(win32.join(env.LOCALAPPDATA, 'Programs', 'Git', 'bin', 'bash.exe'))
+ // And where Scoop installs it: its root for this user (SCOOP, else ~\scoop), then its root for everyone.
+ const scoopRoots = [env.SCOOP, env.USERPROFILE && win32.join(env.USERPROFILE, 'scoop'), env.SCOOP_GLOBAL, env.ProgramData && win32.join(env.ProgramData, 'scoop')]
+ for (const root of scoopRoots) {
+ if (root) candidates.push(win32.join(root, SCOOP_GIT_BASH))
+ }
return candidates.find((candidate) => isFile(candidate)) ?? null
}
diff --git a/cli/src/lib/windows/gitBashTimeout.real.spec.ts b/cli/src/lib/windows/gitBashTimeout.real.spec.ts
new file mode 100644
index 000000000..b2d7e5f27
--- /dev/null
+++ b/cli/src/lib/windows/gitBashTimeout.real.spec.ts
@@ -0,0 +1,41 @@
+// A setup or doctor that outlives its timeout, run in the real Git Bash: what it started ends with it. Windows only, with
+// Git for Windows installed and the host built (the table of who started whom is the host's):
+// cd pty && cargo build
+// cd cli && set HARNESS_PTY_BIN=C:\...\pty\target\debug\harness-pty.exe && npx vitest run src/lib/windows/gitBashTimeout.real.spec.ts
+import { describe, expect, it } from 'vitest'
+import { mkdtempSync, rmSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { runDshCommand } from '../../dsh/shell.js'
+import { processExists } from '../processLiveness.js'
+import { findGitBash } from './gitBash.js'
+import { windowsProcessRows } from './processTable.js'
+
+const enabled = process.platform === 'win32' && !!process.env.HARNESS_PTY_BIN && findGitBash() !== null
+
+describe.skipIf(!enabled)('a Git Bash run stopped at its timeout, on Windows', () => {
+ it('ends what the script started, not only the shell (review of #2025)', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'dsh-gitbash-timeout-'))
+ let child: number | undefined
+ try {
+ // /proc/<pid>/winpid is the Windows pid of a Git Bash process; $! alone is Git Bash's own numbering. Read a second
+ // later, once the fork has exec'd sleep: before, it is the fork's, which exits on its own and would pass the test.
+ const result = await runDshCommand('sleep 120 & sleep 1; echo "child $(cat /proc/$!/winpid)"; wait', { cwd: dir, timeoutMs: 10_000 })
+ expect(result.timedOut).toBe(true)
+ const line = result.lines.find((text) => /^child \d+$/.test(text))
+ expect(line, result.lines.join('\n')).toBeDefined()
+ child = Number(line!.slice('child '.length))
+ const gone = await expect.poll(() => processExists(child!), { timeout: 5_000 }).toBe(false).then(() => true, () => false)
+ if (!gone) {
+ // Say what Windows shows of it, and of its parent, so a failure says why the stop did not reach it.
+ const rows = await windowsProcessRows(null)
+ const own = rows?.find((row) => row.pid === child)
+ const parent = own && rows?.find((row) => row.pid === own.parentPid)
+ expect.fail(`pid ${child} still runs: ${JSON.stringify(own)}; its parent: ${JSON.stringify(parent ?? null)}`)
+ }
+ } finally {
+ if (child) { try { process.kill(child) } catch { /* gone */ } }
+ rmSync(dir, { recursive: true, force: true })
+ }
+ }, 60_000)
+})
diff --git a/cli/src/lib/windows/gitBashTree.spec.ts b/cli/src/lib/windows/gitBashTree.spec.ts
new file mode 100644
index 000000000..022445451
--- /dev/null
+++ b/cli/src/lib/windows/gitBashTree.spec.ts
@@ -0,0 +1,100 @@
+import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import type { ProcessRow } from '../processTable.js'
+import { endGitBashTree, msysTree, type GitBashTreeDeps } from './gitBashTree.js'
+
+const row = (pid: number, parentPid: number, started: number): ProcessRow =>
+ ({ pid, parentPid, executable: 'x.exe', startMarker: `w:${started}`, args: '' })
+
+// The helper in a POSIX bash against a made-up /proc; Windows has no /bin/bash, and there the helper runs in the real Git
+// Bash against its real /proc in gitBashTimeout.real.spec.ts (manual Windows workflow run 40772897725).
+describe.skipIf(process.platform === 'win32')('msysTree', () => {
+ let proc: string
+ beforeEach(() => { proc = mkdtempSync(join(tmpdir(), 'msys-proc-')) })
+ afterEach(() => { rmSync(proc, { recursive: true, force: true }) })
+
+ /** A Git Bash /proc entry: its own pid, its parent's (Git Bash numbering both) and its Windows pid. */
+ const entry = (pid: number, ppid: number, winpid?: number): void => {
+ mkdirSync(join(proc, String(pid)))
+ writeFileSync(join(proc, String(pid), 'ppid'), `${ppid}\n`)
+ if (winpid !== undefined) writeFileSync(join(proc, String(pid), 'winpid'), `${winpid}\n`)
+ }
+
+ it('lists the Windows pids of the run\'s Git Bash tree from its own parent records, nearest first', async () => {
+ // Windows' parent links break where Git Bash starts a program (the sleep's parent there is a process that has
+ // gone); Git Bash's own records keep them, so its tree is read there.
+ entry(10, 1, 1010) // the run's bash, started by the launcher (Windows pid 1000)
+ entry(11, 10, 1011)
+ entry(13, 10, 1013)
+ entry(12, 11, 1012)
+ entry(20, 1, 2020) // someone else's
+ entry(21, 20, 2021)
+ entry(14, 12) // no Windows pid to read: left out
+ expect(await msysTree('/bin/bash', [1000, 1010], proc)).toEqual([1010, 1011, 1013, 1012])
+ })
+
+ it('is empty when no Git Bash process is the run\'s, and null when the helper cannot run or does not finish', async () => {
+ entry(20, 1, 2020)
+ expect(await msysTree('/bin/bash', [1000], proc)).toEqual([])
+ expect(await msysTree(join(proc, 'no-such-bash'), [1000], proc)).toBeNull()
+ const slow = join(proc, 'slow-bash')
+ writeFileSync(slow, '#!/bin/sh\nsleep 30\n', { mode: 0o755 })
+ const started = Date.now()
+ expect(await msysTree(slow, [1000], proc, 200)).toBeNull()
+ expect(Date.now() - started).toBeLessThan(5_000)
+ })
+})
+
+describe('endGitBashTree', () => {
+ // The launcher (100) started the run's bash (101), whose `sleep` (300) Windows shows under a parent that is gone
+ // (999); the sleep started a native program (301). 400 is not the run's.
+ // 50 is a pid Git Bash still lists but Windows has since given to an older process: started before the run did.
+ const rows = [row(100, 1, 10), row(101, 100, 11), row(300, 999, 20), row(301, 300, 21), row(400, 1, 30), row(50, 1, 5)]
+
+ function deps(msys: number[] | null): GitBashTreeDeps & { killed: number[]; reads: number; asked: number[][]; logs: string[] } {
+ const gone = new Set<number>()
+ const d = {
+ killed: [] as number[],
+ reads: 0,
+ asked: [] as number[][],
+ logs: [] as string[],
+ log: (line: string) => { d.logs.push(line) },
+ self: 1,
+ rows: async () => { d.reads++; return rows },
+ kill: (pid: number) => {
+ if (gone.has(pid)) throw Object.assign(new Error('gone'), { code: 'ESRCH' })
+ gone.add(pid)
+ d.killed.push(pid)
+ },
+ msysTree: async (near: number[]) => { d.asked.push(near); return msys },
+ }
+ return d
+ }
+
+ it('ends every process of the run\'s Git Bash tree and what each started, the furthest first, the launcher last', async () => {
+ const d = deps([101, 300])
+ expect(await endGitBashTree(100, d)).toBe('sent')
+ expect(d.asked).toEqual([[100, 101]])
+ expect(d.killed).toEqual([301, 300, 101, 100])
+ // One read of the table, before anything is ended: what is ended first must not hide what it started.
+ expect(d.reads).toBe(1)
+ })
+
+ it('ends no process that is not the one Git Bash listed: one Windows does not show, or one older than the run', async () => {
+ const d = deps([101, 50, 777, 300])
+ await endGitBashTree(100, d)
+ expect(d.killed).toEqual([301, 300, 101, 100])
+ })
+
+ it('when Git Bash\'s records cannot be read, says so and still ends the launcher\'s Windows tree, as before', async () => {
+ const d = deps(null)
+ expect(await endGitBashTree(100, d)).toBe('sent')
+ expect(d.killed).toEqual([101, 100])
+ expect(d.logs).toEqual(["[dsh] could not list Git Bash's processes under pid 100; ending its Windows tree only"])
+ const none = deps([])
+ await endGitBashTree(100, none)
+ expect(none.logs).toEqual([])
+ })
+})
diff --git a/cli/src/lib/windows/gitBashTree.ts b/cli/src/lib/windows/gitBashTree.ts
new file mode 100644
index 000000000..7284c18a9
--- /dev/null
+++ b/cli/src/lib/windows/gitBashTree.ts
@@ -0,0 +1,121 @@
+/**
+ * Ending everything a Git Bash run started, on Windows: a setup or doctor stopped at its timeout, or a viewer stopped
+ * (dsh/shell.ts).
+ *
+ * Windows' own tree is not enough. Git Bash's runtime (MSYS2, a Cygwin) starts a program as a fork and then an exec,
+ * and the program it execs is a new Windows process whose parent is the fork, which then exits: in Windows' table the
+ * program's parent is a process that has gone, so a walk down Windows' parent links (treeKill.ts) never reaches it.
+ * On the manual Windows workflow a `sleep` a script started was still running after the stop (run 40769937293, review
+ * of #2025). Git Bash's own records keep the link: `/proc/<pid>/ppid` is its parent in Git Bash's numbering, which an
+ * exec keeps, and `/proc/<pid>/winpid` is its Windows pid.
+ *
+ * So a helper in the same Git Bash lists the run's tree from those records, and each process listed, with what it
+ * started natively, is ended from one read of Windows' table taken before anything is. Only a pid that table shows,
+ * started no earlier than the launcher, is ended: Windows reuses pids, and one Git Bash listed may since name another,
+ * older process. Without the helper's list (it failed, or did not finish) the launcher's Windows tree alone is ended,
+ * as before, and the log says so.
+ */
+import { spawn } from 'node:child_process'
+import type { ProcessRow } from '../processTable.js'
+import { endProcessTree, processTreeKillDeps, windowsStartTime, type TreeKillDeps, type TreeKillOutcome } from './treeKill.js'
+
+export interface GitBashTreeDeps extends TreeKillDeps {
+ /** The Windows pids of the Git Bash tree under the Git Bash processes among [near], nearest first; null when that
+ * could not be read. */
+ msysTree(near: number[]): Promise<number[] | null>
+ log?: (line: string) => void
+}
+
+/** How long the helper may take: it reads two small files per Git Bash process. */
+export const MSYS_TREE_TIMEOUT_MS = 10_000
+
+/**
+ * $1: the Windows pids to start from, space-separated; $2: Git Bash's /proc. Breadth first, one pass over /proc per
+ * generation, with the shell's own `read` (a fork costs tens of milliseconds there). Bash 3 compatible, as a test runs
+ * it with macOS's bash.
+ */
+const MSYS_TREE_SCRIPT = [
+ 'want=" $1 "; proc=$2; found=" "',
+ 'for p in "$proc"/[0-9]*; do',
+ ' w=; read -r w 2>/dev/null < "$p/winpid"',
+ ' [ -n "$w" ] && case "$want" in *" $w "*) found="$found${p##*/} " ;; esac',
+ 'done',
+ 'frontier=$found',
+ 'while [ -n "${frontier// /}" ]; do',
+ ' next=" "',
+ ' for p in "$proc"/[0-9]*; do',
+ ' q=${p##*/}',
+ ' case "$found" in *" $q "*) continue ;; esac',
+ ' pp=; read -r pp 2>/dev/null < "$p/ppid"',
+ ' [ -n "$pp" ] && case "$frontier" in *" $pp "*) next="$next$q "; found="$found$q " ;; esac',
+ ' done',
+ ' frontier=$next',
+ 'done',
+ 'for q in $found; do w=; read -r w 2>/dev/null < "$proc/$q/winpid"; [ -n "$w" ] && echo "$w"; done',
+ 'exit 0',
+].join('\n')
+
+/** Run the helper in [bash], the same Git Bash the run used: only its own runtime's /proc lists the run's processes. */
+export function msysTree(bash: string, near: number[], proc = '/proc', timeoutMs = MSYS_TREE_TIMEOUT_MS): Promise<number[] | null> {
+ return new Promise((resolve) => {
+ let out = ''
+ let settled = false
+ const done = (value: number[] | null): void => {
+ if (settled) return
+ settled = true
+ clearTimeout(timer)
+ resolve(value)
+ }
+ const timer = setTimeout(() => { helper.kill('SIGKILL'); done(null) }, timeoutMs)
+ timer.unref?.()
+ const helper = spawn(bash, ['--norc', '--noprofile', '-c', MSYS_TREE_SCRIPT, 'msys-tree', near.join(' '), proc], {
+ stdio: ['ignore', 'pipe', 'ignore'],
+ windowsHide: true,
+ })
+ helper.stdout.on('data', (chunk: Buffer) => { out += chunk.toString('utf8') })
+ helper.once('error', () => done(null))
+ helper.once('close', (code) => {
+ if (code !== 0) { done(null); return }
+ done(out.split(/\r?\n/).filter((line) => /^\d+$/.test(line)).map(Number))
+ })
+ })
+}
+
+/**
+ * End the Git Bash run whose launcher is [pid]: every process of its Git Bash tree and what each started, the furthest
+ * first, then the launcher's own Windows tree. Never rejects. The outcome is the launcher's.
+ */
+export async function endGitBashTree(pid: number, deps: GitBashTreeDeps): Promise<TreeKillOutcome> {
+ let rows: ProcessRow[] | null
+ try {
+ rows = await deps.rows()
+ } catch {
+ rows = null
+ }
+ // One read, before anything is ended: ending a process first would hide what it started.
+ const snapshot: TreeKillDeps = { ...deps, rows: async () => rows }
+ const root = rows?.find((row) => row.pid === pid)
+ const rootStart = root ? windowsStartTime(root.startMarker) : null
+ if (!rows || rootStart === null) return endProcessTree(pid, snapshot)
+ const inRun = (row: ProcessRow | undefined): boolean => {
+ const started = row ? windowsStartTime(row.startMarker) : null
+ return started !== null && started >= rootStart
+ }
+ // The launcher (Git for Windows' bin\bash.exe) starts the real bash; either may be the Git Bash process.
+ const near = [pid, ...rows.filter((row) => row.parentPid === pid && inRun(row)).map((row) => row.pid)]
+ const listed = await deps.msysTree(near)
+ if (listed === null) deps.log?.(`[dsh] could not list Git Bash's processes under pid ${pid}; ending its Windows tree only`)
+ const byPid = new Map(rows.map((row) => [row.pid, row]))
+ const members = (listed ?? []).filter((member) => member !== pid && member !== deps.self && inRun(byPid.get(member)))
+ for (const member of members.reverse()) await endProcessTree(member, snapshot)
+ return endProcessTree(pid, snapshot)
+}
+
+/** This computer's: the pty host's table, TerminateProcess, and the helper in [bash] (none without Git Bash). */
+export function gitBashTreeDeps(bash: string | null): GitBashTreeDeps {
+ return {
+ ...processTreeKillDeps(null),
+ msysTree: bash ? (near) => msysTree(bash, near) : async () => [],
+ log: (line) => console.warn(line),
+ }
+}
diff --git a/cli/src/superTabs/machines.spec.ts b/cli/src/superTabs/machines.spec.ts
index ee08e947d..cdc94aa9d 100644
--- a/cli/src/superTabs/machines.spec.ts
+++ b/cli/src/superTabs/machines.spec.ts
@@ -12,6 +12,7 @@ import { startAgent } from './start.js'
import { hostForm, swarmReply } from './fixtures/read/index.js'
import { HostFeed } from './hostFeed.js'
import { SwarmReads } from './swarmReads.js'
+import { processCoreApi } from '../services/processCoreApi.js'
import type { PrivacyObservation } from '../team/privacy.js'
function fixture(env: NodeJS.ProcessEnv = {}, privacy: () => PrivacyObservation | null = () => null, relayed: () => boolean = () => false) {
@@ -1514,7 +1515,7 @@ describe('removed machine cleanup', () => {
live: vi.fn(() => live), all: vi.fn(all),
creationStatus: vi.fn(async (creationId: string) => ({ creationId, state: 'created' as const,
agentId: creationId === 'super-unproven' ? 'someone-else' : `h-${creationId.slice('super-'.length)}` })),
- closeStarted: vi.fn(async (agentId: string) => { live = live.filter(s => s.agentId !== agentId); return { closed: true } as never }),
+ closeMember: vi.fn(async (agentId: string) => { live = live.filter(s => s.agentId !== agentId); return { closed: true } as never }),
} })
const backend = vi.fn(async (_core: unknown, method: string, path: string, _body?: unknown): Promise<unknown> =>
method === 'GET' && path.startsWith('/api/super-tabs/tab/') ? swarmReply(path, { starts }) : { parts: [] })
@@ -1583,7 +1584,7 @@ describe('removed machine cleanup', () => {
// The person's repository forgets only the Swarm worktree it held.
const listed = git(m.own, 'worktree', 'list', '--porcelain')
expect(listed).toContain('/mine\n'); expect(listed).not.toContain('/earlier\n')
- expect(m.core.agents.closeStarted).toHaveBeenCalledWith('h-recorded', 'now')
+ expect(m.core.agents.closeMember).toHaveBeenCalledWith('h-recorded', undefined)
expect(m.acks()).toEqual([[m.core, 'POST', '/api/super-tabs/tab/commands',
{ operationId: 'machine-cleaned-removal', command: { type: 'machine.cleaned', machineId: 'host', removalId: 'removal' } }]])
expect(JSON.parse(readFileSync(swarmWorktreesFile(m.data, 'host'), 'utf8')).worktrees.map((w: { tabId: string }) => w.tabId)).toEqual(['other-tab'])
@@ -1603,7 +1604,7 @@ describe('removed machine cleanup', () => {
const host = m.open()
await host.clean('tab', 'removal')
for (const kept of [join(m.data, 'swarm-projects', 'tab'), m.newRoot, m.recorded, m.earlier]) expect(existsSync(kept), kept).toBe(true)
- expect(m.core.agents.closeStarted).not.toHaveBeenCalled()
+ expect(m.core.agents.closeMember).not.toHaveBeenCalled()
expect(m.acks()).toEqual([])
expect(m.log.mock.calls.flat().join('\n')).toContain('added to that Swarm again')
// Settled: the next poll's row, if any, does not run it again.
@@ -1628,7 +1629,7 @@ describe('removed machine cleanup', () => {
addedOnRead(m, 2)
await m.open().clean('tab', 'removal')
for (const kept of [join(m.data, 'swarm-projects', 'tab'), m.newRoot, m.recorded, m.earlier]) expect(existsSync(kept), kept).toBe(true)
- expect(m.core.agents.closeStarted).not.toHaveBeenCalled()
+ expect(m.core.agents.closeMember).not.toHaveBeenCalled()
expect(records(m)).toEqual(['tab', 'other-tab'])
expect(m.acks()).toEqual([])
})
@@ -1654,7 +1655,7 @@ describe('removed machine cleanup', () => {
if (method !== 'GET' || !path.startsWith('/api/super-tabs/tab/')) return { parts: [] }
return swarmReply(path, { starts: m.starts, ...(added ? { machines: [{ machineId: 'host', contributorId: 'person', cwd: m.newRoot }] } : {}) })
})
- vi.mocked(m.core.agents.closeStarted!).mockImplementation(async () => {
+ vi.mocked(m.core.agents.closeMember!).mockImplementation(async () => {
// The close was slow; the owner re-added the machine while it was awaited and its new start reused this clone.
added = true
m.setLive([])
@@ -1716,16 +1717,80 @@ describe('removed machine cleanup', () => {
expect(m.acks()).toHaveLength(1)
})
- it('keeps a folder whose session will not close, cleans the rest and acknowledges', async () => {
+ // Diego 10-29: a folder kept for a session still running in it was confirmed cleaned anyway, and its records dropped, so
+ // nothing ever tried it again. A kept folder now leaves the cleanup unconfirmed, retried on the usual backoff.
+ it('keeps a folder whose session will not close, cleans the rest and confirms nothing until it is gone', async () => {
const m = machine()
const person = session('person', join(m.newRoot), undefined)
m.setLive([person])
- await m.open().clean('tab', 'removal')
- expect(m.core.agents.closeStarted).not.toHaveBeenCalledWith('person', 'now')
+ const host = m.open()
+ await expect(host.clean('tab', 'removal')).rejects.toThrow('a session still runs in tab.')
+ expect(vi.mocked(m.core.agents.closeMember!).mock.calls.map(([agentId]) => agentId)).not.toContain('person')
expect(existsSync(m.newRoot)).toBe(true)
expect(existsSync(join(m.data, 'swarm-projects', 'tab'))).toBe(false)
- expect(m.log.mock.calls.flat().join('\n')).toContain(m.newRoot)
+ expect(m.log.mock.calls.flat().join('\n')).toContain('Kept tab: a session still runs in it.')
+ expect(m.log.mock.calls.flat().join('\n')).not.toContain(m.home)
+ expect(m.acks()).toEqual([])
+ expect(records(m)).toEqual(['tab', 'other-tab'])
+ // The person closed it: the next try after the backoff deletes the folder and confirms.
+ m.setLive([])
+ m.step(60_000)
+ await host.clean('tab', 'removal')
+ expect(existsSync(m.newRoot)).toBe(false)
expect(m.acks()).toHaveLength(1)
+ expect(records(m)).toEqual(['other-tab'])
+ })
+
+ // Diego 10-29: under harnessd the Swarm runs in its own process, whose core API (services/processCoreApi.ts) has no
+ // closeStarted, so the agents were never closed and their folders were kept. It closes through the Swarm's own close.
+ it('closes the Swarm agents in its folders and deletes them when the Swarm runs in its own process', async () => {
+ const m = machine()
+ const cloned = join(m.data, 'swarm-projects', 'tab', 'project')
+ const starts = [...m.starts, { id: 'cloned', creationId: 'super-cloned-0123456789', harnessId: 'h-cloned', request: { machineId: 'host', worktree: false, cwd: cloned } }]
+ m.backend.mockImplementation(async (_core, method, path) => method === 'GET' && path.startsWith('/api/super-tabs/tab/') ? swarmReply(path, { starts }) : { parts: [] })
+ let live = [session('h-recorded', m.recorded, 'agent'), session('h-cloned', cloned, 'agent')]
+ const asked: [string, Record<string, unknown>][] = []
+ const ask = vi.fn(async (query: string, payload: Record<string, unknown>): Promise<Record<string, unknown>> => {
+ asked.push([query, payload])
+ if (query === 'creation_status') return { observation: { creationId: payload.creationId, state: 'created', agentId: 'h-cloned' } }
+ if (query !== 'close_member') return { error: 'UNKNOWN_QUERY' }
+ live = live.filter(s => s.agentId !== payload.agentId)
+ return { closed: true }
+ })
+ const core = processCoreApi(m.data, 'superTabs', { live: () => live, ask })
+ await new ContributedMachines(core, 'host', { backend: m.backend, log: m.log, now: () => 0 } as never).clean('tab', 'removal')
+ expect(asked.filter(([query]) => query === 'close_member').map(([, payload]) => payload).sort((a, b) => String(a.agentId).localeCompare(String(b.agentId))))
+ .toEqual([{ agentId: 'h-cloned', creationId: 'super-cloned-0123456789' }, { agentId: 'h-recorded' }])
+ for (const gone of [join(m.data, 'swarm-projects', 'tab'), m.recorded]) expect(existsSync(gone), gone).toBe(false)
+ expect(m.acks()).toHaveLength(1)
+ expect(records(m)).toEqual(['other-tab'])
+ })
+
+ // Review of #2070: the close's answer was thrown away, so a removal that never confirmed said nothing of why.
+ it.each([
+ ['refuses', (m: ReturnType<typeof machine>) => vi.mocked(m.core.agents.closeMember!).mockResolvedValue({ error: 'SERVICE_UNAVAILABLE' } as never), 'SERVICE_UNAVAILABLE'],
+ // Re-check of #2070: an error's own text can carry a full path, so a failed close is logged by a fixed code.
+ ['fails', (m: ReturnType<typeof machine>) => vi.mocked(m.core.agents.closeMember!).mockRejectedValue(new Error(`EACCES: ${m.recorded}`)), 'CLOSE_FAILED'],
+ ])('confirms nothing, keeps the records and logs why while a Swarm agent in the clone %s to close', async (_how, fail, reason) => {
+ const m = machine()
+ fail(m)
+ await expect(m.open().clean('tab', 'removal')).rejects.toThrow('a session still runs in recorded.')
+ expect(m.core.agents.closeMember).toHaveBeenCalledWith('h-recorded', undefined)
+ expect(existsSync(m.recorded)).toBe(true)
+ expect(m.acks()).toEqual([])
+ expect(records(m)).toEqual(['tab', 'other-tab'])
+ expect(m.log.mock.calls.flat().filter(line => String(line).startsWith('Kept recorded:')))
+ .toEqual([`Kept recorded: a session still runs in it; its close was refused: ${reason}.`])
+ })
+
+ // Review of #2070: both of a tab's clone roots end in its id, and the message named it twice.
+ it('names each kept folder once', async () => {
+ const m = machine()
+ const cloned = join(m.data, 'swarm-projects', 'tab', 'project')
+ m.setLive([session('h-recorded', m.recorded, 'agent'), session('person', cloned), session('another-person', m.newRoot)])
+ vi.mocked(m.core.agents.closeMember!).mockResolvedValue({ error: 'NOT_A_MEMBER' } as never)
+ await expect(m.open().clean('tab', 'removal')).rejects.toThrow(
+ new Error('Did not finish cleaning up Swarm tab yet: a session still runs in recorded, tab. It tries again later.'))
})
it('finishes a cleanup after a restart that interrupted its acknowledgement', async () => {
diff --git a/cli/src/superTabs/machines.ts b/cli/src/superTabs/machines.ts
index 83f4c8fc1..b418e5662 100644
--- a/cli/src/superTabs/machines.ts
+++ b/cli/src/superTabs/machines.ts
@@ -698,13 +698,15 @@ export class ContributedMachines {
if (addedAgain(state)) return
const starts = list(state.starts)
const swarm = new Set(recorded.map(w => w.harnessId))
+ /** Each Swarm start's receipt on this machine, by the agent it names: what lets the core close one its host no longer retains. */
+ const receipts = new Map<string, string>()
const candidates = recorded.map(w => ({ source: w.source, path: w.path, startId: w.startId, proven: true }))
for (const start of starts) {
const request = object(start.request)
if (request.machineId !== this.machineId || !identifier(start.id) || !identifier(start.harnessId) || !identifier(start.creationId)) continue
const creation = await this.core.agents.creationStatus?.(start.creationId)
const proven = creation?.state === 'created' && creation.agentId === start.harnessId
- if (proven) swarm.add(start.harnessId)
+ if (proven) { swarm.add(start.harnessId); receipts.set(start.harnessId, start.creationId) }
const agent = this.core.agents.all().find(a => a.agentId === start.harnessId)
if (request.worktree === true && typeof request.cwd === 'string' && agent?.cwd && !candidates.some(c => c.path === agent.cwd)) {
candidates.push({ source: request.cwd, path: agent.cwd, startId: start.id, proven: proven && agent.startedBy === 'agent' })
@@ -713,25 +715,42 @@ export class ContributedMachines {
const worktrees: string[] = []
for (const c of candidates) {
if (c.proven && await swarmWorktree(c.path, c.source, c.startId)) worktrees.push(c.path)
- else if (existsSync(c.path)) this.log(`Kept ${c.path}: it is not provably a worktree a Swarm start made.`)
+ else if (existsSync(c.path)) this.log(`Kept ${basename(c.path)}: it is not provably a worktree a Swarm start made.`)
}
if (addedAgain(await read())) return
const targets = [...worktrees, ...swarmCloneRoots(this.core.dataDir, tabId)]
const inside = (cwd: string | null) => !!cwd && targets.some(t => within(t, cwd))
+ // The Swarm's own close (core/api.ts closeMember), which the core answers in its process and in the Swarm's own
+ // (services/processCoreApi.ts). Diego 10-29: closeStarted is the agent cleanup's alone, so under harnessd the Swarm's
+ // process had none, closed nothing and kept every folder. The core closes only a member its host retains or one this
+ // start's receipt names; any other agent-started session here stays open, and its folder with it.
+ // Why each close failed, by the folder it ran in: review of #2070, a removal that never confirmed said nothing of why.
+ const refused = new Map<string, string>()
for (const agent of this.core.agents.live()) {
if (agent.startedBy === 'agent' && (swarm.has(agent.agentId) || inside(agent.cwd))) {
- await this.core.agents.closeStarted?.(agent.agentId, 'now').catch(() => null)
+ const answer = await (this.core.agents.closeMember?.(agent.agentId, receipts.get(agent.agentId)) ?? Promise.resolve({ error: 'UNSUPPORTED' }))
+ // A fixed code, not the error's own text, which can carry a full path (re-check of #2070).
+ .catch(() => ({ error: 'CLOSE_FAILED' }))
+ if (typeof answer.error === 'string' && agent.cwd) refused.set(agent.cwd, answer.error)
}
}
const open = this.core.agents.live().map(a => a.cwd).filter((cwd): cwd is string => !!cwd)
+ const kept = new Set<string>()
for (const target of targets) {
- if (open.some(cwd => within(target, cwd))) { this.log(`Kept ${target}: a session still runs in it.`); continue }
+ if (open.some(cwd => within(target, cwd))) {
+ const reasons = [...new Set([...refused].filter(([cwd]) => within(target, cwd)).map(([, error]) => error))]
+ this.log(`Kept ${basename(target)}: a session still runs in it${reasons.length ? `; its close was refused: ${reasons.join(', ')}` : ''}.`)
+ kept.add(basename(target)); continue
+ }
if (addedAgain(await read())) return
const source = candidates.find(c => c.path === target)?.source
// git forgets the worktree in its repository too; a folder it refuses (a submodule, a lock) is deleted as a folder.
if (source) await git(source, ['worktree', 'remove', '--force', target]).catch(() => null)
await rm(target, { recursive: true, force: true })
}
+ // Diego 10-29: a folder kept for a live session was confirmed cleaned anyway and its records dropped, so nothing ever
+ // tried it again. Unconfirmed, the cleanup row stays and the next try comes on clean()'s backoff.
+ if (kept.size) throw new Error(`Did not finish cleaning up Swarm ${tabId} yet: a session still runs in ${[...kept].join(', ')}. It tries again later.`)
if (addedAgain(await read())) return
writeWorktrees(file, readWorktrees(file).filter(w => w.tabId !== tabId))
await this.deps.backend(this.core, 'POST', `/api/super-tabs/${tabId}/commands`, { operationId: `machine-cleaned-${removalId}`,
diff --git a/desktop/lib/super_tabs/model.dart b/desktop/lib/super_tabs/model.dart
index e9eabc0ce..62577043e 100644
--- a/desktop/lib/super_tabs/model.dart
+++ b/desktop/lib/super_tabs/model.dart
@@ -277,6 +277,7 @@ class SuperTabSnapshot {
}),
localModels = SuperTabLocalModels.read(row['localModels']),
mentionsWaiting = (row['mentionsWaiting'] as num?)?.toInt() ?? 0,
+ mentionsDigest = row['mentionsDigest'] as String?,
decisionsWaiting = (row['decisionsWaiting'] as num?)?.toInt() ?? 0,
decisionsDigest = row['decisionsDigest'] as String?,
buddiesEnabled = row['buddies'] == true,
@@ -339,6 +340,10 @@ class SuperTabSnapshot {
/// before tags (spec 2026-10-24-lead-mentions.md).
final int mentionsWaiting;
+ /// Which tags wait, as a digest that moves when they change though the
+ /// count may not; null from a backend before it (superTabs/mentions.ts).
+ final String? mentionsDigest;
+
/// Decisions asked of the reader and not yet answered, Swarm-wide; 0 from a
/// backend before decisions (backend superTabs/decisions.ts).
final int decisionsWaiting;
diff --git a/desktop/lib/super_tabs/notifications.dart b/desktop/lib/super_tabs/notifications.dart
index ed66adcf5..d372bcc54 100644
--- a/desktop/lib/super_tabs/notifications.dart
+++ b/desktop/lib/super_tabs/notifications.dart
@@ -62,7 +62,8 @@ void Function(SuperTabMention)? superTabMentionNotice(
);
/// The reader's tags in one Swarm: Waiting and Earlier, read from the backend
-/// whenever the Swarm changes while a tag waits or the count moves. One per
+/// when the count or the digest of which tags wait moves (or, from a backend
+/// without the digest, whenever the Swarm changes while a tag waits). One per
/// controller.
class SuperTabMentions extends ChangeNotifier {
SuperTabMentions._(this.controller) {
@@ -82,6 +83,7 @@ class SuperTabMentions extends ChangeNotifier {
List<SuperTabMention> waiting = const [], earlier = const [];
String? error;
int? _count;
+ String? _digest;
int _lastChangedAt = -1;
int? _epoch;
// Null until the first read: tags waiting then are not news.
@@ -98,9 +100,11 @@ class SuperTabMentions extends ChangeNotifier {
String get _path =>
'/api/super-tabs/${Uri.encodeComponent(controller.tabId)}';
- /// A tag can arrive while another is answered, leaving the count as it was,
- /// so any change of the Swarm is read while a tag waits. With none waiting
- /// and none answered (an idle Swarm, or a backend before tags) nothing is.
+ /// A tag can arrive while another is answered, leaving the count as it was:
+ /// the digest moves then. A backend without the digest leaves only the
+ /// change of the Swarm to go by, so any is read while a tag waits, and so is
+ /// any after a failed read. With none waiting and none answered (an idle
+ /// Swarm, or a backend before tags) nothing is.
void _changed() {
final snapshot = controller.snapshot;
if (snapshot == null) return;
@@ -117,16 +121,20 @@ class SuperTabMentions extends ChangeNotifier {
}
_lastChangedAt = controller.lastChangedAt;
_count = snapshot.mentionsWaiting;
+ _digest = snapshot.mentionsDigest;
unawaited(load());
return;
}
}
// A tag that expires moves the count without changing the Swarm.
final count = snapshot.mentionsWaiting;
- final moved = count != _count;
- if (!moved && controller.lastChangedAt == _lastChangedAt) return;
+ final digest = snapshot.mentionsDigest;
+ final moved = count != _count || digest != _digest;
+ final changed = controller.lastChangedAt != _lastChangedAt;
+ if (!moved && !(changed && (digest == null || error != null))) return;
_lastChangedAt = controller.lastChangedAt;
_count = count;
+ _digest = digest;
if (count == 0 && (!moved || _seen == null)) {
_seen ??= {};
return;
@@ -173,6 +181,10 @@ class SuperTabMentions extends ChangeNotifier {
// Read before who may read what changed: the read asked again since
// replaces it.
if (epoch != _epoch) return;
+ final same =
+ error == null &&
+ _sameMentions(waiting, nextWaiting) &&
+ _sameMentions(earlier, nextEarlier);
waiting = nextWaiting;
earlier = nextEarlier;
error = null;
@@ -183,6 +195,8 @@ class SuperTabMentions extends ChangeNotifier {
if (!seen.contains(m.id)) onArrived?.call(m);
}
}
+ // The same lists again: nothing to show, so nobody is told.
+ if (same) return;
} catch (_) {
if (epoch != _epoch) return;
error = 'Could not load notifications. Try again.';
@@ -213,6 +227,28 @@ class SuperTabMentions extends ChangeNotifier {
}
}
+/// Whether [a] and [b] show the same tags the same way.
+bool _sameMentions(List<SuperTabMention> a, List<SuperTabMention> b) {
+ if (a.length != b.length) return false;
+ for (var i = 0; i < a.length; i++) {
+ final (x, y) = (a[i], b[i]);
+ if (x.id != y.id ||
+ x.projectId != y.projectId ||
+ x.projectName != y.projectName ||
+ x.conversationId != y.conversationId ||
+ x.conversationTitle != y.conversationTitle ||
+ x.messageId != y.messageId ||
+ x.leadId != y.leadId ||
+ x.leadName != y.leadName ||
+ x.text != y.text ||
+ x.createdAt != y.createdAt ||
+ x.resolvedBy != y.resolvedBy) {
+ return false;
+ }
+ }
+ return true;
+}
+
/// The reader's tags across the Swarm: Waiting first, then Earlier, newest
/// first in each. Opening one goes to its message.
class SuperTabNotifications extends StatefulWidget {
diff --git a/desktop/lib/super_tabs/parts.dart b/desktop/lib/super_tabs/parts.dart
index 0ad5813c0..2c7d76a58 100644
--- a/desktop/lib/super_tabs/parts.dart
+++ b/desktop/lib/super_tabs/parts.dart
@@ -270,6 +270,9 @@ class SuperTabParts {
if (_derived['mentionsWaiting'] case final num waiting) {
state['mentionsWaiting'] = waiting.toInt();
}
+ if (_derived['mentionsDigest'] case final String digest) {
+ state['mentionsDigest'] = digest;
+ }
if (_derived['decisionsWaiting'] case final num waiting) {
state['decisionsWaiting'] = waiting.toInt();
}
diff --git a/desktop/test/super_tab_mentions_test.dart b/desktop/test/super_tab_mentions_test.dart
index a6199ba96..fe1ec8c82 100644
--- a/desktop/test/super_tab_mentions_test.dart
+++ b/desktop/test/super_tab_mentions_test.dart
@@ -314,6 +314,83 @@ void main() {
controller.dispose();
});
+ test('a Swarm change that leaves the count and digest alone reads no '
+ 'tags and tells nobody', () async {
+ final server = ConversationServer()..tag('m1', text: 'First');
+ server.state['mentionsDigest'] = 'digest-1';
+ final controller = server.controller();
+ addTearDown(controller.dispose);
+ final mentions = SuperTabMentions.of(controller);
+ await controller.refresh();
+ await pumpEventQueue();
+ expect(mentions.waiting.map((m) => m.text), ['First']);
+ final reads = server.mentionReads;
+ var notified = 0;
+ mentions.addListener(() => notified++);
+ for (var i = 0; i < 5; i++) {
+ final before = controller.lastChangedAt;
+ server.append('Unrelated $i');
+ await controller.refresh();
+ await pumpEventQueue();
+ expect(controller.lastChangedAt, isNot(before));
+ }
+ expect(server.mentionReads - reads, 0);
+ expect(notified, 0);
+
+ // One answered and another arrived: the count stays, the digest moves.
+ server.mentions.single
+ ..['resolvedAt'] = '2026-10-24T05:10:00Z'
+ ..['resolvedBy'] = 'reply';
+ server.tag('m2', text: 'Second');
+ server.state['mentionsWaiting'] = 1;
+ server.state['mentionsDigest'] = 'digest-2';
+ await controller.refresh();
+ await pumpEventQueue();
+ // Waiting once and Earlier once.
+ expect(server.mentionReads - reads, 2);
+ expect(notified, 1);
+ expect(mentions.waiting.map((m) => m.text), ['Second']);
+ expect(mentions.earlier.map((m) => m.text), ['First']);
+ });
+
+ test('a failed read is tried again on the next Swarm change, digest or '
+ 'not', () async {
+ final server = ConversationServer()..tag('m1', text: 'First');
+ server.state['mentionsDigest'] = 'digest-1';
+ server.failMentions = true;
+ final controller = server.controller();
+ addTearDown(controller.dispose);
+ final mentions = SuperTabMentions.of(controller);
+ await controller.refresh();
+ await pumpEventQueue();
+ expect(mentions.error, isNotNull);
+ server.failMentions = false;
+ server.append('Unrelated');
+ await controller.refresh();
+ await pumpEventQueue();
+ expect(mentions.error, isNull);
+ expect(mentions.waiting.map((m) => m.text), ['First']);
+ });
+
+ test('without a digest, every Swarm change while a tag waits reads the '
+ 'lists again, and the same lists tell nobody', () async {
+ final server = ConversationServer()..tag('m1', text: 'First');
+ final controller = server.controller();
+ addTearDown(controller.dispose);
+ final mentions = SuperTabMentions.of(controller);
+ await controller.refresh();
+ await pumpEventQueue();
+ final reads = server.mentionReads;
+ var notified = 0;
+ mentions.addListener(() => notified++);
+ server.append('Unrelated');
+ await controller.refresh();
+ await pumpEventQueue();
+ expect(server.mentionReads - reads, 2);
+ expect(notified, 0);
+ expect(mentions.waiting.map((m) => m.text), ['First']);
+ });
+
test(
'a backend without the list shows an empty list, not an error',
() async {
diff --git a/desktop/test/support/super_tab_fixture.dart b/desktop/test/support/super_tab_fixture.dart
index 0e02d4242..c86a37d32 100644
--- a/desktop/test/support/super_tab_fixture.dart
+++ b/desktop/test/support/super_tab_fixture.dart
@@ -144,6 +144,8 @@ class SwarmPartsBackend {
'machines': <String, dynamic>{},
if (state['mentionsWaiting'] != null)
'mentionsWaiting': state['mentionsWaiting'],
+ if (state['mentionsDigest'] != null)
+ 'mentionsDigest': state['mentionsDigest'],
if (state['decisionsWaiting'] != null)
'decisionsWaiting': state['decisionsWaiting'],
if (state['decisionsDigest'] != null)