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.

Merge batch 11-04 b1517: #2950 #2960 #2953 (#2969)

0xken150e9bd99970ba
diff --git a/backend/integration/superTabBuddies.test.ts b/backend/integration/superTabBuddies.test.ts
index f5f4d3c02..34a83831a 100644
--- a/backend/integration/superTabBuddies.test.ts
+++ b/backend/integration/superTabBuddies.test.ts
@@ -955,8 +955,8 @@ describe('a Buddy hears how its person\'s requests go, unasked (Diego, 10-29)',
it('hands a Buddy a claimed row without the machine it names (security review 2 of #2178)', async () => {
const { f, agentId, proposal, pm } = await proposed()
const answer = String((await pm('Taking it.', { replyTo: proposal })).result.messageId)
- // A machine-leaving row's shape (machines.ts removalEnded), as if it were news: what reaches the Buddy names no machine.
- await db.superTabMessage.update({ where: { id: answer }, data: { text: 'Studio Mac left the Swarm.', source: { kind: 'machine-left', machine: 'Studio Mac' } } })
+ // A machine-removed row's shape (commands.ts removeMachine), as if it were news: what reaches the Buddy names no machine.
+ await db.superTabMessage.update({ where: { id: answer }, data: { text: 'Studio Mac left the Swarm.', source: { kind: 'machine-removed', machine: 'Studio Mac' } } })
const news = (await f.store.deliveries(peer, f.tab, agentId)).find(d => d.messageId === answer)!
const claimed = await f.store.delivery(peer, f.tab, agentId, news.id, { type: 'claim', claimId: randomUUID() })
expect(JSON.stringify(claimed.message)).not.toContain('Studio Mac')
diff --git a/backend/integration/superTabBuddyProbes.test.ts b/backend/integration/superTabBuddyProbes.test.ts
index c6249299d..17e0e7109 100644
--- a/backend/integration/superTabBuddyProbes.test.ts
+++ b/backend/integration/superTabBuddyProbes.test.ts
@@ -215,10 +215,10 @@ it('reads a machine leaving the Swarm without its name, in the project chat and
await f.store.execute(owner, f.tab, { operationId: `machine-cleaned-${removalId}`, command: { type: 'machine.cleaned', machineId: 'probe-studio', removalId } })
const pm = [...(await f.store.messages(owner, f.tab, 'lead', {}, f.lead)).messages, ...(await f.store.messages(owner, f.tab, group, {}, f.lead)).messages]
- const left = pm.find(m => m.text.startsWith('OWNER-STUDIO-HOST left the Swarm'))!, removed = pm.find(m => m.text.includes('removed OWNER-STUDIO-HOST from the Swarm'))!
- expect([left, removed].map(m => (m.source as { kind?: string }).kind)).toEqual(['machine-left', 'machine-removed'])
+ // Only the row in the task's group: no chat says the machine left (#2742).
+ expect(pm.filter(m => m.text.includes('OWNER-STUDIO-HOST')).map(m => (m.source as { kind?: string }).kind)).toEqual(['machine-removed'])
+ const removed = pm.find(m => m.text.includes('removed OWNER-STUDIO-HOST from the Swarm'))!
const seen = [...(await f.store.messages(peer, f.tab, 'lead', {}, agentId)).messages, ...(await f.store.messages(peer, f.tab, group, {}, agentId)).messages]
- expect(seen.find(m => m.id === left.id)!.text).toBe('A machine left the Swarm: its host confirmed its cleanup.')
expect(seen.find(m => m.id === removed.id)!.text).toMatch(/^owner removed a machine from the Swarm, and \S+ with it\.$/)
expect(JSON.stringify(seen)).not.toContain('OWNER-STUDIO-HOST')
})
diff --git a/backend/integration/superTabInbox.test.ts b/backend/integration/superTabInbox.test.ts
index 29952f459..492f67260 100644
--- a/backend/integration/superTabInbox.test.ts
+++ b/backend/integration/superTabInbox.test.ts
@@ -917,3 +917,60 @@ describe('reading many thread ranges at once', () => {
expect(filterOf(reads.find(e => !e.command?.hint)!)).not.toHaveProperty('$or')
}, 60_000)
})
+
+// anh Dee, 11-04 (#2742): "remove these messages", the "<machine> left the Swarm: ..." lines in every project chat. Those already
+// stored are read by nobody, and a project chat whose newest row is one does not stay unread.
+describe('a machine-left line stored before #2742', () => {
+ const left = { text: 'Studio left the Swarm: its host confirmed its cleanup.', author: personAuthor(owner),
+ source: { kind: 'machine-left', machine: 'Studio' } }
+ /** The project chat as a person's first load serves it, with their read mark there. */
+ const served = async (tabId: string, as: AuthUser) => {
+ const data = (await get(`/api/super-tabs/${tabId}/parts`, as)).body.data
+ const chat = (data.groups as any[]).find(g => g.key === 'lead')!.data
+ return { lastMainSequence: chat.lastMainSequence as number, mark: (data.derived.readMarks ?? {}).lead as number | undefined }
+ }
+
+ it('is in no page and no preview', async () => {
+ const s = await scene()
+ await send(s.tabId, 'lead', 'Project latest')
+ await insert(s.tabId, 'lead', [left])
+ for (const as of [owner, peer]) {
+ const page = (await get(`/api/super-tabs/${s.tabId}/conversations/lead/messages`, as)).body.data.messages as any[]
+ expect(page.map(m => m.text)).toContain('Project latest')
+ expect(JSON.stringify(page)).not.toContain('left the Swarm')
+ expect(latestOf((await inbox(s.tabId, as)).body.data).lead).toBe('Project latest')
+ }
+ })
+
+ it('leaves a person who had read the chat with it read, though their app sends no read', async () => {
+ const s = await scene()
+ const latest = await send(s.tabId, 'lead', 'Project latest')
+ await markRead(s.tabId, 'lead', latest.sequence, peer)
+ await insert(s.tabId, 'lead', [left])
+ // Desktop and the phone count a chat unread while its newest main row is past their mark, and send no read at or below
+ // the mark the server holds: the chat's newest main row is served as the newest one people read.
+ expect(await served(s.tabId, peer)).toEqual({ lastMainSequence: latest.sequence, mark: latest.sequence })
+ // Asked again, from what this server already learned, the answer is the same.
+ expect((await served(s.tabId, peer)).lastMainSequence).toBe(latest.sequence)
+ })
+
+ it('serves a poll the same number when the project chat is sent again, as for a reply in a thread', async () => {
+ const s = await scene()
+ const latest = await send(s.tabId, 'lead', 'Project latest')
+ await markRead(s.tabId, 'lead', latest.sequence, peer)
+ await insert(s.tabId, 'lead', [left])
+ const { cursor } = (await get(`/api/super-tabs/${s.tabId}/parts`, peer)).body.data
+ // A reply only in a thread: the chat comes again whole from the change feed, its newest main row still the hidden line.
+ await send(s.tabId, 'lead', 'A reply', { replyTo: latest.id })
+ const changes = (await get(`/api/super-tabs/${s.tabId}/changes?cursor=${cursor}`, peer)).body.data
+ const chat = (changes.groups as any[]).find(g => g.key === 'lead')
+ expect(chat?.data.lastMainSequence).toBe(latest.sequence)
+ })
+
+ it('serves a row people read after the line as the chat\'s newest', async () => {
+ const s = await scene()
+ await insert(s.tabId, 'lead', [left])
+ const after = await send(s.tabId, 'lead', 'After the line')
+ expect((await served(s.tabId, peer)).lastMainSequence).toBe(after.sequence)
+ })
+})
diff --git a/backend/integration/superTabLiveness.test.ts b/backend/integration/superTabLiveness.test.ts
index 67e330014..f9f5eca04 100644
--- a/backend/integration/superTabLiveness.test.ts
+++ b/backend/integration/superTabLiveness.test.ts
@@ -9,6 +9,7 @@ import { PrismaClient, type Prisma, type SuperTabDelivery } from '@prisma/client
import type { AuthUser } from '../src/lib/ssoAuth.js'
import { SuperTabStore, type Polled, type SuperTabStoreHooks } from '../src/superTabs/store.js'
import { SWEEP_EVERY_MS } from '../src/superTabs/liveness.js'
+import { DOWN_REMOVE_MS } from '../src/superTabs/closedAgents.js'
import { stableId } from '../src/superTabs/model.js'
import type { Command } from '../src/superTabs/commands.js'
import type { MachineObservation } from '../src/superTabs/machines.js'
@@ -2347,19 +2348,21 @@ describe('an agent closed on its machine', () => {
expect(await ids(f)).toContain(idle)
})
+ // A session its machine lists stopped takes no task at all (closedAgents.ts sessionClosed, task #2744); one it has only
+ // stopped listing can still be assigned while the report that removes it after DOWN_REMOVE_MS is being written.
it('does not lose an assignment that lands while it is being removed (the report retries against it)', async () => {
const f = await swarm()
await f.beat(f.clock.now)
const idle = await f.add(f.group, 'test-host', f.harness.other, 'Idle one')
const second = String((await f.execute({ type: 'conversation.create', title: 'Second', brief: 'More', checks: [{ description: 'Verified', independent: false }] })).result.conversationId)
- await reports(f, 30, [f.harness.worker], [f.harness.other])
+ await reports(f, DOWN_REMOVE_MS / 1000, [f.harness.worker])
let raced = false
const racing = new SuperTabStore(beforeFirstWrite(db, async () => {
if (raced) return
raced = true
await f.execute({ type: 'work.assign', conversationId: second, agentId: idle })
}), () => f.clock.now)
- await reports(f, 10, [f.harness.worker], [f.harness.other], racing)
+ await reports(f, 10, [f.harness.worker], [], racing)
expect(raced).toBe(true)
const state = (await f.timed.read(owner, f.tab)).state
const group = state.conversations.find(c => c.id === second)!
diff --git a/backend/integration/superTabTeam.test.ts b/backend/integration/superTabTeam.test.ts
index e860f4eac..575017cd1 100644
--- a/backend/integration/superTabTeam.test.ts
+++ b/backend/integration/superTabTeam.test.ts
@@ -771,11 +771,11 @@ describe('one machine: a Mac and its Harness Team account', () => {
// A host repeating its confirmation gets its own receipt back.
await expect(cleaned(person, 'personal-mac')).resolves.toBeDefined()
await expect(cleaned(team, 'team-mac')).resolves.toBeDefined()
- expect((await store.messages(person, tab, 'lead', {})).messages.filter(m => m.text.includes('left the Swarm')).map(m => m.text))
- .toEqual(['MacBook Pro left the Swarm: its host confirmed its cleanup.'])
+ // No chat says it left (anh Dee, 11-04, #2742).
+ expect((await store.messages(person, tab, 'lead', {})).messages.filter(m => m.text.includes('left the Swarm')).map(m => m.text)).toEqual([])
})
- it('leaves 30 minutes after its removal while its host keeps reporting without confirming, and the project\'s chat says why', async () => {
+ it('leaves 30 minutes after its removal while its host keeps reporting without confirming, and no chat says so', async () => {
let now = Date.now()
const timed = new SuperTabStore(db, () => now)
const tab = (await timed.create(owner, randomUUID(), 'Silent host')).id
@@ -801,12 +801,12 @@ describe('one machine: a Mac and its Harness Team account', () => {
now = removedAt + 30 * 60_000
await report()
expect(await listed()).toEqual([])
- expect(await said()).toEqual(['MacBook Pro left the Swarm: its host did not confirm its cleanup within 30 minutes. It finishes the cleanup when it next connects.'])
- // Its cleanup still reaches its host, whose late confirmation closes it without another line.
+ expect(await said()).toEqual([])
+ // Its cleanup still reaches its host, whose late confirmation closes it, still without a line.
expect((await rosterRows(timed, person, 'personal-mac')).filter(r => r.tabId === tab && 'cleanup' in r)).toEqual([{ tabId: tab, cleanup: { machineId: 'personal-mac', removalId } }])
await timed.execute(person, tab, { operationId: `machine-cleaned-${removalId}`, command: { type: 'machine.cleaned', machineId: 'personal-mac', removalId } })
expect((await storedState(tab)).machineCleanups).toBeUndefined()
- expect(await said()).toHaveLength(1)
+ expect(await said()).toEqual([])
})
// Diego's Mac, 2026-10-28, after #1955: the Mac and its team account were removed together and went past the fallback
diff --git a/backend/src/github/app.test.ts b/backend/src/github/app.test.ts
index 48964c5e8..92281c3f8 100644
--- a/backend/src/github/app.test.ts
+++ b/backend/src/github/app.test.ts
@@ -178,6 +178,20 @@ describe('installation tokens', () => {
expect(minted(github)).toEqual([{ repositories: ['app'], permissions: today }])
})
+ it('says so, with the installation and why, each time the grant cannot be read', async () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+ try {
+ const { app, github, time } = setup()
+ await app.createToken(77, repo)
+ expect(warn).toHaveBeenCalledWith("[github] installation 77's permissions unreadable (HTTP 404); agent tokens ask for today's set alone")
+ time.advance(60_001)
+ github.control.down = true
+ await expect(app.createToken(77, repo)).rejects.toMatchObject({ code: 'GITHUB_UNAVAILABLE' })
+ expect(warn).toHaveBeenLastCalledWith("[github] installation 77's permissions unreadable (GITHUB_UNAVAILABLE); agent tokens ask for today's set alone")
+ expect(warn.mock.calls.flat().join(' ')).not.toContain('ghs_')
+ } finally { warn.mockRestore() }
+ })
+
it('remembers an installation\'s grant ten minutes', async () => {
const { app, github, time } = setup()
github.grants.set(77, { ...today, workflows: 'write' })
diff --git a/backend/src/github/app.ts b/backend/src/github/app.ts
index 16b4a6371..9588588f4 100644
--- a/backend/src/github/app.ts
+++ b/backend/src/github/app.ts
@@ -346,9 +346,13 @@ export class GitHubApp {
if (answer.status === 200 && listed && typeof listed === 'object' && !Array.isArray(listed)) {
permissions = Object.fromEntries(Object.entries(listed as Record<string, unknown>)
.filter((entry): entry is [string, 'read' | 'write'] => entry[1] === 'read' || entry[1] === 'write'))
+ } else {
+ // Said every time, as agents then lose workflow pushes and issue writes with nothing else to show why (review 2 of #2956).
+ console.warn(`[github] installation ${installationId}'s permissions unreadable (HTTP ${answer.status}); agent tokens ask for today's set alone`)
}
} catch (error) {
if (!(error instanceof GitHubAppError) || error.code === 'GITHUB_APP_MISCONFIGURED') throw error
+ console.warn(`[github] installation ${installationId}'s permissions unreadable (${error.code}); agent tokens ask for today's set alone`)
// GitHub did not answer: today's set, asked again next time.
return null
}
diff --git a/backend/src/superTabs/closedAgents.ts b/backend/src/superTabs/closedAgents.ts
index 20f9b1b8b..f4acb523a 100644
--- a/backend/src/superTabs/closedAgents.ts
+++ b/backend/src/superTabs/closedAgents.ts
@@ -21,6 +21,24 @@ export const STOPPED_REMOVE_MS = 30_000
const removable = (state: SuperTabState, target: Agent): boolean =>
!isBuddy(target) && target.workDecision !== 'keep' && target.workDecision !== 'stop' && !ledProject(state, target.id)
+/** Whether [report] proves [target]'s session stopped: listed stopped and, for a reservation a resume renewed, stopped in that
+ * very generation, since an older host lists the earlier run stopped and cannot say which. */
+const provedStopped = (state: SuperTabState, target: Agent, report: Pick<MachineObservation, 'stoppedHarnessIds' | 'stoppedReservations'>): boolean => {
+ if (!report.stoppedHarnessIds.includes(target.harnessId)) return false
+ const reservation = state.harnessReservations?.find(r => r.machineId === target.machineId && r.harnessId === target.harnessId)
+ return !reservation || !reservation.renewed || !!report.stoppedReservations?.some(r => r.harnessId === target.harnessId && r.generation === reservation.generation)
+}
+
+/** Whether [target]'s machine, reading now, proves its idle cleanup closed its session: a fresh reading that lists it closed
+ * and stopped, not running. Such an agent takes no task (commands.ts work.assign): on 11-04 the idle cleanup closed Tern on
+ * the Mac Studio and the PM assigned it six seconds later, so the task was lost at once (task #2744). A session merely
+ * listed stopped may be a Change agent before its rebind, or a host mid-restart, and keeps taking work (review of #2953). */
+export function sessionClosed(state: SuperTabState, target: Agent, now: number): boolean {
+ const reading = state.machines?.find(m => m.machineId === target.machineId)?.observation
+ return !!reading && now - reading.receivedAt <= MACHINE_FRESH_MS && !reading.runningHarnessIds.includes(target.harnessId)
+ && !!reading.closedHarnessIds?.includes(target.harnessId) && provedStopped(state, target, reading)
+}
+
/**
* The agents of [machineId] that [report], arriving now over the reading in [state], shows closed: not listed running, down
* for a while, and either proved stopped (the proof the slot count accepts, machines.ts machineSlots; STOPPED_REMOVE_MS) or
@@ -40,12 +58,7 @@ export function closedAgents(state: SuperTabState, machineId: string, report: Pi
if (!removable(state, target)) return false
if (!target.downSince) return false
const down = now - Date.parse(target.downSince)
- if (report.stoppedHarnessIds.includes(target.harnessId)) {
- const reservation = state.harnessReservations?.find(r => r.machineId === machineId && r.harnessId === target.harnessId)
- // Older hosts can prove a first launch stopped, but cannot tell a resumed runtime from its predecessor.
- return down >= STOPPED_REMOVE_MS
- && (!reservation || !reservation.renewed || !!report.stoppedReservations?.some(r => r.harnessId === target.harnessId && r.generation === reservation.generation))
- }
+ if (report.stoppedHarnessIds.includes(target.harnessId)) return down >= STOPPED_REMOVE_MS && provedStopped(state, target, report)
return down >= DOWN_REMOVE_MS && report.runningHarnessIds.length > 0 && fresh.runningHarnessIds.length > 0
})
}
diff --git a/backend/src/superTabs/commands.test.ts b/backend/src/superTabs/commands.test.ts
index ee171c202..3499c2986 100644
--- a/backend/src/superTabs/commands.test.ts
+++ b/backend/src/superTabs/commands.test.ts
@@ -3219,7 +3219,8 @@ describe('machine.remove', () => {
})
// Diego's Mac, 2026-10-28: a host that keeps reporting but never confirms held its machine 'Cleaning up' for a day.
- it('drops the machine\'s records after 30 minutes without an acknowledgement, says so in the project\'s chat, and keeps the cleanup for its host', () => {
+ // anh Dee, 11-04 (#2742): the Machines list shows which machines are in the Swarm; no chat says one left.
+ it('drops the machine\'s records after 30 minutes without an acknowledgement, says nothing in any chat, and keeps the cleanup for its host', () => {
const { f, removalId } = cleaning()
const day = 30 * 60_000
// Its host keeps reporting, so only the fallback can expire it.
@@ -3227,9 +3228,7 @@ describe('machine.remove', () => {
expect(f.run({ type: 'tab.rename', name: 'Still cleaning' }, owner, { now: 1000 + day - 1 }).messages).toEqual([])
expect(f.state.machines!.map(m => m.removal?.id)).toEqual([removalId])
expect(visibleState(f.state, owner, 1000 + day).machines).toEqual([])
- expect(f.run({ type: 'tab.rename', name: 'Half an hour later' }, owner, { now: 1000 + day }).messages).toEqual([
- expect.objectContaining({ conversationId: 'lead', source: { kind: 'machine-left', machine: 'Studio' },
- text: 'Studio left the Swarm: its host did not confirm its cleanup within 30 minutes. It finishes the cleanup when it next connects.' })])
+ expect(f.run({ type: 'tab.rename', name: 'Half an hour later' }, owner, { now: 1000 + day }).messages).toEqual([])
expect(f.state.machines).toEqual([])
expect(f.state.machineCleanups).toEqual([{ machineId: 'host', removalId, machineOwnerId: 'owner', requestedAt: new Date(1000).toISOString() }])
const add = { type: 'machine.contribute', machineId: 'host', name: 'Studio', cwd: '/repo', engines: ['claude'], maxAgents: 2 } as Command
@@ -3309,20 +3308,11 @@ describe('machine.remove', () => {
expect(f.state.machineCleanups).toBeUndefined()
})
- it('tells each project\'s chat that the machine left once its host confirmed the cleanup', () => {
+ it('says nothing in any project\'s chat once its host confirmed the cleanup', () => {
const { f, removalId } = cleaning()
- const docs = f.run({ type: 'project.create', name: 'Docs', visibility: 'public', brief: '' }).result as { conversationId: string }
- expect(f.run(cleaned(removalId)).messages).toEqual(['lead', docs.conversationId].map(conversationId => expect.objectContaining({
- conversationId, source: { kind: 'machine-left', machine: 'Studio' }, recipients: [], text: 'Studio left the Swarm: its host confirmed its cleanup.' })))
- })
-
- it('writes the line only in the chats of projects its machine owner can open', () => {
- const f = fixture()
- f.invite()
- contribute(f, 'peer')
- f.run({ type: 'project.create', name: 'Secret', visibility: 'private', brief: '' })
- const removalId = f.run({ type: 'machine.remove', machineId: 'host' }, peer).result.removalId as string
- expect(f.run(cleaned(removalId), peer).messages.map(m => [m.conversationId, m.author.id])).toEqual([['lead', 'peer']])
+ f.run({ type: 'project.create', name: 'Docs', visibility: 'public', brief: '' })
+ expect(f.run(cleaned(removalId)).messages).toEqual([])
+ expect(f.state.machines).toEqual([])
})
it.each([['has no reading', null], ['was last read long ago', 1000 - 45_001]] as const)(
@@ -3333,8 +3323,7 @@ describe('machine.remove', () => {
expect(visibleState(f.state, owner).machines).toHaveLength(1)
const effects = f.run({ type: 'machine.remove', machineId: 'host' })
const removalId = effects.result.removalId as string
- expect(effects.messages).toEqual([expect.objectContaining({ conversationId: 'lead', source: { kind: 'machine-left', machine: 'Studio' },
- text: 'Studio left the Swarm: it is offline. It cleans up when it next connects.' })])
+ expect(effects.messages.filter(m => m.source?.kind === 'machine-left')).toEqual([])
expect(f.state.machines).toEqual([])
expect(f.state.machineCleanups).toEqual([{ machineId: 'host', removalId, machineOwnerId: 'owner', requestedAt: new Date(1000).toISOString() }])
expect(visibleState(f.state, owner).machines).toEqual([])
@@ -3371,9 +3360,8 @@ describe('machine.remove', () => {
expect(visibleState(f.state, owner, later).machines).toEqual([])
expect(visibleState(f.state, owner, later).machineCleanups).toBeUndefined()
expect(f.state.machines).toEqual([expect.objectContaining({ machineId: 'host' })])
- // The next change applies the same rule, says why, and keeps the cleanup for its host.
- expect(f.run({ type: 'tab.rename', name: 'Still cleaning' }, owner, { now: later }).messages).toEqual([expect.objectContaining({
- conversationId: 'lead', text: 'Studio left the Swarm: it is offline. It cleans up when it next connects.' })])
+ // The next change applies the same rule, says nothing, and keeps the cleanup for its host.
+ expect(f.run({ type: 'tab.rename', name: 'Still cleaning' }, owner, { now: later }).messages).toEqual([])
expect(f.state.machines).toEqual([])
expect(f.state.machineCleanups).toEqual([{ machineId: 'host', removalId, machineOwnerId: 'owner', requestedAt: new Date(1000).toISOString() }])
f.run(cleaned(removalId))
@@ -5407,3 +5395,41 @@ describe('the Next-up queue (work.queue)', () => {
expect(f.state).toEqual(once)
})
})
+
+// 11-04 13:57 VN on the Mac Studio: the CLI's idle cleanup closed Tern, its machine's next reading listed the session
+// stopped, and six seconds after the close the PM assigned Tern a task, which was lost at once (task #2744).
+describe('assigning an agent whose session was closed', () => {
+ /** A worker on machine 'host', whose reading at [receivedAt] lists [running] and [stopped], and of those [cleaned] as closed
+ * by its idle cleanup. */
+ function closed(running: string[], stopped: string[], receivedAt = 1000, cleaned: string[] = stopped) {
+ const f = fixture(), group = f.group(), worker = f.add('worker', 'owner', group)
+ f.state.machines = [{ machineId: 'host', name: 'Studio', contributorId: 'owner', membershipGeneration: f.state.people.find(p => p.id === 'owner')!.generation,
+ generation: 'g1', cwd: '/repo', engines: ['codex'], maxAgents: 4, paused: false, observation: { generation: 'g1', receivedAt,
+ resources: { cpuPercent: null, memoryTotalBytes: null, memoryUsedBytes: null, diskFreeBytes: null, diskTotalBytes: null, projectDiskFreeBytes: null,
+ deviceType: null, chip: null, os: null, gpuPercent: null }, tools: {}, quotas: [], runningHarnessIds: running, stoppedHarnessIds: stopped,
+ ...(cleaned.length ? { closedHarnessIds: cleaned } : {}) } }]
+ return { f, group, worker }
+ }
+
+ it('is refused, saying to start a new agent, and changes nothing', () => {
+ const { f, group, worker } = closed([], ['worker'])
+ const before = JSON.stringify(f.state)
+ expect(() => f.run({ type: 'work.assign', conversationId: group, agentId: worker }))
+ .toThrow(expect.objectContaining({ code: 'AGENT_CLOSED', message: expect.stringContaining('session was closed. Start a new agent') }))
+ expect(JSON.stringify(f.state)).toBe(before)
+ })
+
+ // The review of #2953: Change agent stops the old terminal before the rebind moves the member to its new one, and a host
+ // mid-restart lists its sessions stopped for a moment. Either is stopped, never closed by the cleanup, and keeps its task.
+ it('goes ahead when the reading lists it running, is stale, lists it stopped but not closed, or cannot prove a resumed session stopped', () => {
+ for (const { f, group, worker } of [closed(['worker'], []), closed(['worker'], ['worker']), closed([], ['worker'], -100_000), closed([], ['worker'], 1000, [])]) {
+ f.run({ type: 'work.assign', conversationId: group, agentId: worker })
+ expect(f.state.conversations.find(c => c.id === group)!.assignment).toMatchObject({ agentId: worker, status: 'working' })
+ }
+ // A reservation renewed by a resume: an older host lists the earlier run stopped and cannot say which (closedAgents.ts).
+ const resumed = closed([], ['worker'])
+ resumed.f.state.harnessReservations = [{ machineId: 'host', harnessId: 'worker', generation: 'g2', renewed: true } as never]
+ resumed.f.run({ type: 'work.assign', conversationId: resumed.group, agentId: resumed.worker })
+ expect(resumed.f.state.conversations.find(c => c.id === resumed.group)!.assignment).toMatchObject({ agentId: resumed.worker })
+ })
+})
diff --git a/backend/src/superTabs/commands.ts b/backend/src/superTabs/commands.ts
index 92f55d98b..d8617aef2 100644
--- a/backend/src/superTabs/commands.ts
+++ b/backend/src/superTabs/commands.ts
@@ -1,6 +1,6 @@
import { applyPushOrigin, failPushUnknown, failTeamWritten, hasStep, mainAccountMembers, markTeamWritten, requireNameable, requireTeamWrittenAccount, requireTeamWrittenMachine, TEAM_WRITTEN_REASON, TEAM_WRITTEN_STOP, teamWrittenTask } from './teamWritten.js'
import type { PushOrigin } from '../github/pushOrigin.js'
-import { admitMachineStart, defaultProfileId, storedProfileId, BUDDY_WINDOWS_REFUSAL, buddyHomeFolder, buddyProjectsOn, SWARM_BUDDY_CAPABILITY, cleanupOverdue, dropIdleBuddyMachines, machinesFull, MACHINES_FULL_MESSAGE, NOT_SWARM_MACHINE, downSince, expireRemoval, expireRemovals, failTeamWork, removalEnded, machineContribution, machineOffline, machineSlots, contributedEngines, ownedMachine, productManagerOn, projectFolder, projectsOnMachine, requireTeamWork, teamMachineFor, teamAccountPool, teamPoolsOf, teamStartRequest, teamWorkCheck, type ContributedMachine } from './machines.js'
+import { admitMachineStart, defaultProfileId, storedProfileId, BUDDY_WINDOWS_REFUSAL, buddyHomeFolder, buddyProjectsOn, SWARM_BUDDY_CAPABILITY, cleanupOverdue, dropIdleBuddyMachines, machinesFull, MACHINES_FULL_MESSAGE, NOT_SWARM_MACHINE, downSince, expireRemoval, expireRemovals, failTeamWork, machineContribution, machineOffline, machineSlots, contributedEngines, ownedMachine, productManagerOn, projectFolder, projectsOnMachine, requireTeamWork, teamMachineFor, teamAccountPool, teamPoolsOf, teamStartRequest, teamWorkCheck, type ContributedMachine } from './machines.js'
import { agentDisplayName, assignAgentDisplayNames } from './agentNames.js'
import { keepUsernames, setUsername, swarmUsernames, usernameOf } from './usernames.js'
import { setName, writtenNameOf } from './personNames.js'
@@ -44,7 +44,7 @@ import { bindPullRequestCards, bindTaskCard, pullRequestCard, recordCardMerged }
import { avatarChoice, setAgentAvatar, setMemberAvatar, setStatus, setTimeZone, statusSchema, type AvatarUpload } from './profile.js'
import { cancelledTags, cancelReason, switchExpired, unfinishedWork } from './tidy.js'
import { BUILT_IN_ROLE_DEFAULTS, agentProjectId, audienceOf, groupRepositoryUrl, canReadAgent, canReadProject, deleteProject, markConfidential, onTeamAccount, requireSingleRegistration, groupProject, inviteLanding, ledProject, manageProject, MAX_ROLES, mutableProjects, placeRules, projectAgent, projectMembers, projectOf, projectsOf, readAgent, readConversation, readProject, requestedByLine, sameProject, sharedWithLeads, withRoleDefaults, type SwarmProject, PROJECT_STATUS_MAX } from './projects.js'
-import { recordClosing } from './closedAgents.js'
+import { recordClosing, sessionClosed } from './closedAgents.js'
import { declinedWith, declinedWithout } from './sharing.js'
import { actorLabel, memberChanges, memberRowText, quotedName } from './memberRows.js'
import { BUDDY_SESSION_CAPABILITY, chatOwner, endSessionEntry, entryProject, liveSessions, mainBuddyOf, MAX_SESSION_WAITING, requireChatOwner, SESSION_ENDED_KIND, sessionAgentsOf, sessionOf, setSessions, type SessionEndReason } from './buddySessions.js'
@@ -811,8 +811,7 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,
const state = cloneData(original)
numberConversations(state)
placeRules(state)
- const ended = expireRemovals(state, context.now)
- // Their removal already said they left; nothing is said again.
+ expireRemovals(state, context.now)
const deleted = new Set(context.deletedMachineIds ?? [])
if (deleted.size && state.machineCleanups) {
state.machineCleanups = state.machineCleanups.filter(c => !deleted.has(c.machineId))
@@ -820,7 +819,7 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,
}
assignAgentDisplayNames(state.agents)
const result: Record<string, unknown> = {}
- const effects: CommandEffects = { state, messages: ended, cancelledAgents: [], previousAttempts: [], routeDecisions: [], result }
+ const effects: CommandEffects = { state, messages: [], cancelledAgents: [], previousAttempts: [], routeDecisions: [], result }
/** Agents this command took out of the Swarm with a row of its own in each of their chats (agent.remove, removeMachine). */
const announcedRemovals = new Set<string>()
const operationId = stableId(state.id, actorKey(actor), context.operationId)
@@ -879,8 +878,6 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,
const cleanup = state.machineCleanups?.find(c => owns(c.machineId, c.removalId, c.machineOwnerId))
// Of a deleted Swarm, only a host with a cleanup waiting learns it exists (deletion.ts).
if (!pool && !cleanup) state.deletion ? notThere() : fail('NOT_FOUND', 'This machine has no cleanup waiting.', 404)
- // One that already left the list said why then.
- if (pool) effects.messages.push(...removalEnded(state, pool, 'confirmed'))
state.machines = state.machines?.filter(m => m !== pool)
state.machineCleanups = state.machineCleanups?.filter(c => c !== cleanup)
if (!state.machineCleanups?.length) delete state.machineCleanups
@@ -1437,7 +1434,7 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,
}
// Its host is offline and cannot acknowledge, so the fallback applies at once: the machine leaves the list
// now and its cleanup waits for its next connection (spec 2026-10-23-remove-machine.md).
- if (pool?.removal && machineOffline(pool, context.now)) effects.messages.push(...removalEnded(state, pool, expireRemoval(state, pool, context.now)))
+ if (pool?.removal && machineOffline(pool, context.now)) expireRemoval(state, pool)
return removed
}
@@ -3328,6 +3325,11 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,
if (state.paused) fail('PAUSED', 'New work is paused in this Super Tab.')
if (activeWork(group)) fail('WORK_ACTIVE', 'The current assignment must finish or be reconciled before reassignment.')
const worker = agent(state, command.agentId)
+ // Before anything changes: a task given to a closed session is lost at once, and its agent then stays listed until the
+ // task is handed on (closedAgents.ts removeClosedAgents). Without a task it leaves on its machine's next report.
+ if (sessionClosed(state, worker, context.now)) {
+ fail('AGENT_CLOSED', `${worker.displayName ?? worker.name}'s session was closed. Start a new agent for this task.`)
+ }
projectChatJoin(group, worker.id, worker.displayName ?? worker.name)
assignable(worker)
sameProject(state, group, worker)
diff --git a/backend/src/superTabs/liveness.ts b/backend/src/superTabs/liveness.ts
index d6bd684aa..8aeb1b54f 100644
--- a/backend/src/superTabs/liveness.ts
+++ b/backend/src/superTabs/liveness.ts
@@ -363,7 +363,7 @@ function buddyNotice(state: SuperTabState, start: AgentStart): MessageDraft[] {
}
/** A Product Manager's start that stopped trying ([text], leadStart.ts), said once as a system row of its project's
- * conversation, written under the person who asked for it as a machine's removal is (machines.ts removalEnded). Nothing while
+ * conversation, written under the person who asked for it. Nothing while
* it still waits for a machine. */
function leadNotice(state: SuperTabState, start: AgentStart, text: string | null): MessageDraft[] {
const asker = start.request.leadFor!
diff --git a/backend/src/superTabs/machines.test.ts b/backend/src/superTabs/machines.test.ts
index bba9f245d..befd3ada2 100644
--- a/backend/src/superTabs/machines.test.ts
+++ b/backend/src/superTabs/machines.test.ts
@@ -15,6 +15,12 @@ it('accepts optional tool probe failures while preserving older observations', (
expect(machineObservationSchema.safeParse({ ...sent, tools: { git: true }, toolProbeErrors: { flutter: 'interactive shell timed out' } }).success).toBe(true)
})
+it('takes the sessions its idle cleanup closed, bounded as the stopped ones are', () => {
+ const { receivedAt: _receivedAt, ...sent } = observation
+ expect(machineObservationSchema.safeParse({ ...sent, stoppedHarnessIds: ['a'], closedHarnessIds: ['a'] }).success).toBe(true)
+ expect(machineObservationSchema.safeParse({ ...sent, closedHarnessIds: Array(1001).fill('a') }).success).toBe(false)
+})
+
it('takes a model that is starting, and never counts it as served for a Builder', () => {
const { receivedAt: _receivedAt, ...sent } = observation
const model = { grid: 'swarm-0123456789ab', model: 'qwen3', kind: 'local' as const }
@@ -270,6 +276,9 @@ describe('a machine\'s reading as a report stores it (DB 2.5)', () => {
expect(same({})).toBe(true)
expect(same({ resources: { ...sent.resources, cpuPercent: 90 }, tools: { git: true }, harnessTokens: { a: tokens } })).toBe(true)
expect(same({ harnessProgress: { a: 9 } })).toBe(true)
+ // Which stopped sessions its idle cleanup closed decides who may be assigned (closedAgents.ts sessionClosed, task #2744).
+ expect(same({ stoppedHarnessIds: ['a'], closedHarnessIds: ['a'] })).toBe(false)
+ expect(readingOnly(stored({ stoppedHarnessIds: ['a'] }), stored({ stoppedHarnessIds: ['a'], closedHarnessIds: ['a'] }))).toBe(false)
expect(readingOnly(stored({ runningHarnessIds: ['a', 'b'] }), stored({ runningHarnessIds: ['b', 'a'] }))).toBe(true)
// A decision may differ.
expect(same({ runningHarnessIds: ['a', 'b'] })).toBe(false)
diff --git a/backend/src/superTabs/machines.ts b/backend/src/superTabs/machines.ts
index fbc63e225..949764b07 100644
--- a/backend/src/superTabs/machines.ts
+++ b/backend/src/superTabs/machines.ts
@@ -33,7 +33,7 @@ export interface ContributedMachine {
hostMachineId?: string
/** Set by machine.remove: an online machine stays listed, paused, while its host deletes the Swarm's folders there, until
* the host says it cleaned up (machine.cleaned) or REMOVAL_FALLBACK_MS passes. An offline one leaves the list at once and
- * its cleanup waits for its host (expireRemovals). Each project's chat hears how it ended (removalEnded). */
+ * its cleanup waits for its host (expireRemovals). No chat hears how it ended (#2742). */
removal?: { id: string; state: 'cleaning'; requestedAt: string }
/** Added by Desktop as its person joined the Swarm, not lent to teammates by choice: their work runs here only through
* its Harness Team account whose privacy check passed, whether or not the Swarm requires that everywhere
@@ -72,8 +72,6 @@ export const REMOVAL_FALLBACK_MS = 30 * 60_000
* said 'still cleaning up' for good. */
export const cleanupOverdue = (cleanup: MachineCleanup, now: number): boolean =>
!cleanup.requestedAt || now - Date.parse(cleanup.requestedAt) >= REMOVAL_FALLBACK_MS
-/** Why a removal ended: its host confirmed, it was offline, or it kept reporting without confirming for REMOVAL_FALLBACK_MS. */
-export type RemovalEnd = 'confirmed' | 'offline' | 'timedOut'
/** Older contributions may name engines that Swarm no longer starts. Their fresh
* observation lists the supported engines that are actually installed. */
/** The default account's id for an engine on a machine: the CLI and Desktop compute the same 16 hex (sha256 of `machine:engine:default`). */
@@ -137,6 +135,10 @@ export const machineObservationSchema = z.object({ generation: z.string().max(16
quotaReasons: z.record(z.string().regex(/^[a-z][a-z0-9_-]{0,79}$/), z.string().min(1).max(300)).refine(v => Object.keys(v).length <= 8).optional(),
runningHarnessIds: z.array(z.string().max(160)).max(1000),
stoppedHarnessIds: z.array(z.string().max(160)).max(1000),
+ /** Of the stopped ones, those this machine's idle cleanup closed (cli superTabs/cleanupCloses.ts): only such a session is
+ * refused new work (closedAgents.ts sessionClosed), never one stopped by Change agent or a restart (review of #2953).
+ * Absent from hosts before it. */
+ closedHarnessIds: z.array(z.string().max(160)).max(1000).optional(),
stoppedReservations: z.array(z.object({ harnessId: z.string().min(1).max(160), generation: z.string().min(1).max(160) }).strict()).max(1000).optional(),
/** When each running agent's screen or transcript last changed (epoch ms); its lease renews on recent progress
* (liveness.ts). Absent from hosts before it, whose agents renew by running alone. */
@@ -239,6 +241,7 @@ export function readingOnly(previous: StoredReading | undefined, next: StoredRea
// A refused start fails in the full report, which reads the Swarm's starts (store.ts fullReading).
if (!previous || next.stoppedReservations?.length || next.refusedStarts?.length || previous.sessionKind !== next.sessionKind) return false
if (!sameSet(previous.runningHarnessIds, next.runningHarnessIds) || !sameSet(previous.stoppedHarnessIds, next.stoppedHarnessIds)
+ || !sameSet(previous.closedHarnessIds ?? [], next.closedHarnessIds ?? [])
|| !sameSet(previous.preparingStartIds, next.preparingStartIds)) return false
if (!previous.harnessProgress || !next.harnessProgress) return !previous.harnessProgress && !next.harnessProgress
const before = previous.harnessProgress, after = next.harnessProgress
@@ -633,37 +636,18 @@ export function machineOffline(pool: ContributedMachine, now: number): boolean {
}
/** A removal its host cannot acknowledge (offline) or has not acknowledged in REMOVAL_FALLBACK_MS: the machine's records go, and the
- * cleanup waits for its host on its next connection. */
-export function expireRemoval(state: SuperTabState, pool: ContributedMachine, now: number): RemovalEnd {
+ * cleanup waits for its host on its next connection. No chat is told: the Machines list shows which machines are in the Swarm
+ * (anh Dee, 11-04, #2742). */
+export function expireRemoval(state: SuperTabState, pool: ContributedMachine): void {
(state.machineCleanups ??= []).push({ machineId: pool.machineId, removalId: pool.removal!.id, machineOwnerId: pool.contributorId,
requestedAt: pool.removal!.requestedAt })
state.machines = state.machines!.filter(m => m !== pool)
- return machineOffline(pool, now) ? 'offline' : 'timedOut'
-}
-
-/** The removals that ended, with the lines saying why. A read applies them without writing and drops the lines. */
-export function expireRemovals(state: SuperTabState, now: number): MessageDraft[] {
- return (state.machines?.filter(m => m.removal
- && (machineOffline(m, now) || now - Date.parse(m.removal.requestedAt) >= REMOVAL_FALLBACK_MS)) ?? [])
- .flatMap(pool => removalEnded(state, pool, expireRemoval(state, pool, now)))
-}
-
-/** One line in each project's chat when a machine's removal ends, saying why (2026-10-28), so a machine that leaves the
- * Machines list without its host's confirmation does not go unexplained. Written as the machine's owner, or the Swarm's
- * owner once they left, only where that person can open the project. A Harness Team account follows its Mac and says
- * nothing of its own. */
-export function removalEnded(state: SuperTabState, pool: ContributedMachine, how: RemovalEnd): MessageDraft[] {
- const writer = state.people.find(p => p.id === pool.contributorId) ?? state.people.find(p => p.id === state.ownerId)
- if (pool.hostMachineId || !pool.removal || !writer) return []
- const why = how === 'confirmed' ? 'its host confirmed its cleanup.'
- : how === 'offline' ? 'it is offline. It cleans up when it next connects.'
- : `its host did not confirm its cleanup within ${REMOVAL_FALLBACK_MS / 60_000} minutes. It finishes the cleanup when it next connects.`
- const reader: Actor = { kind: 'person', userId: writer.id, email: writer.email }
- return projectsOf(state).filter(p => canReadProject(state, reader, p) && state.conversations.some(c => c.id === p.leadConversationId)).map(p => ({
- id: stableId('machine-removal-ended', pool.removal!.id, pool.machineId, p.leadConversationId), conversationId: p.leadConversationId,
- author: { kind: 'person', id: writer.id, name: writer.email, membershipGeneration: writer.generation },
- // Its own kind, naming the machine, so a Buddy reads it without the name (store.ts withoutMachineNames).
- text: `${pool.name} left the Swarm: ${why}`, replyTo: null, recipients: [], source: { kind: 'machine-left', machine: pool.name } }))
+}
+
+/** Ends the removals that are due. A read applies them without writing. */
+export function expireRemovals(state: SuperTabState, now: number): void {
+ for (const pool of state.machines?.filter(m => m.removal
+ && (machineOffline(m, now) || now - Date.parse(m.removal.requestedAt) >= REMOVAL_FALLBACK_MS)) ?? []) expireRemoval(state, pool)
}
/** Pending intents reserve slots too. Only an explicit host stop frees a launched slot. A Mac and its Harness Team account
diff --git a/backend/src/superTabs/model.ts b/backend/src/superTabs/model.ts
index fd5984eed..72b612b36 100644
--- a/backend/src/superTabs/model.ts
+++ b/backend/src/superTabs/model.ts
@@ -616,7 +616,10 @@ export const NOTICE_READERS = {
// A member removed one of the coordinator's notes: told to the coordinator alone (instructions.ts).
'note-removed': 'recipients',
'project-report': 'people', route: 'people', 'route-override': 'people', 'buddy-proposal': 'people', 'buddy-proposal-sent': 'people', 'buddy-session-ended': 'people',
- 'decision-delegated': 'people', 'machine-removed': 'people', 'machine-left': 'people', ship: 'people',
+ 'decision-delegated': 'people', 'machine-removed': 'people', ship: 'people',
+ // "<machine> left the Swarm: ..." in every project chat, no longer written (anh Dee, 11-04, #2742): the rows stored before are
+ // read by nobody (they have no recipients), and a project chat ending in one is served as read (read/servedTops.ts).
+ 'machine-left': 'agent',
// A task's one line in its project chat, edited as it moves (taskLine.ts): people read a task's state there.
'task-line': 'people',
// A project PM's private line in a person's one Buddy chat, marked with its project (commands.ts person.tell): its person
@@ -965,6 +968,7 @@ export function visibleState(state: SuperTabState, actor: Actor, now = Date.now(
if (!pool.observation) continue
pool.observation.runningHarnessIds = pool.observation.runningHarnessIds.filter(id => harnesses.has(id))
pool.observation.stoppedHarnessIds = pool.observation.stoppedHarnessIds.filter(id => harnesses.has(id))
+ if (pool.observation.closedHarnessIds) pool.observation.closedHarnessIds = pool.observation.closedHarnessIds.filter(id => harnesses.has(id))
if (pool.observation.harnessProgress) pool.observation.harnessProgress = Object.fromEntries(Object.entries(pool.observation.harnessProgress).filter(([id]) => harnesses.has(id)))
// Each attempt's tokens are read through the cost summary (costs/read.ts), never from the machine's raw counts.
delete pool.observation.harnessTokens
diff --git a/backend/src/superTabs/notices.test.ts b/backend/src/superTabs/notices.test.ts
index c10b43529..a5670ae05 100644
--- a/backend/src/superTabs/notices.test.ts
+++ b/backend/src/superTabs/notices.test.ts
@@ -84,8 +84,9 @@ describe('the notices the Swarm writes into a chat', () => {
expect(unclassified, 'add each to NOTICE_READERS in model.ts: who reads it?').toEqual([])
// A kind no longer written keeps its readers for the rows stored before: the server's idle stop (project instructions,
// phase 4) told only the Lead; the coordinator's progress post, now the project's status card (Dee, 2026-10-31), people;
- // the Shipped line, now the task's own line (taskLine.ts; Quiet Swarm, Dee, 2026-11-02), people.
- const retired = ['idle-stop', 'project-report', 'ship']
+ // the Shipped line, now the task's own line (taskLine.ts; Quiet Swarm, Dee, 2026-11-02), people; a machine's removal ending,
+ // no longer said in any chat (anh Dee, 11-04, #2742), nobody.
+ const retired = ['idle-stop', 'project-report', 'ship', 'machine-left']
expect(Object.keys(NOTICE_READERS).filter(kind => !written.has(kind) && !retired.includes(kind))).toEqual([])
expect(retired.filter(kind => written.has(kind))).toEqual([])
})
@@ -99,6 +100,8 @@ describe('the notices the Swarm writes into a chat', () => {
expect(agentOnly({ kind }, [])).toBe(false)
}
expect(agentOnly({ kind: 'agent-only' }, [])).toBe(true)
+ // The "<machine> left the Swarm" lines stored before #2742 have no recipients: nobody reads them.
+ expect(agentOnly({ kind: 'machine-left', machine: 'Studio' }, [])).toBe(true)
// A row of a kind people read can still be its recipients' alone; a kind no code knows (stored by a newer server) shows.
expect(agentOnly(forRecipients({ kind: 'system' }), [])).toBe(true)
expect(agentOnly({ kind: 'from-a-newer-server' }, ['lead'])).toBe(false)
diff --git a/backend/src/superTabs/read/inbox.ts b/backend/src/superTabs/read/inbox.ts
index 1af436ce4..2b56e1269 100644
--- a/backend/src/superTabs/read/inbox.ts
+++ b/backend/src/superTabs/read/inbox.ts
@@ -200,7 +200,7 @@ const newestIn = (tabId: string, conversationId: string, sequence: Record<string
/** Each chat's newest main message the person reads: the row at its stored main sequence, read alone; the LATEST_SCAN rows
* before it only for a chat where that row is not one (an older chat's newest row may be a thread reply or a hidden copy).
* `more` when such a chat has none in that range. */
-async function latestIn(db: PrismaClient, tabId: string, state: SuperTabState, chats: Conversation[]): Promise<{ latest: Map<string, Row>; more: boolean }> {
+export async function latestIn(db: PrismaClient, tabId: string, state: SuperTabState, chats: Conversation[]): Promise<{ latest: Map<string, Row>; more: boolean }> {
const tops = chats.flatMap(g => { const top = g.lastMainSequence ?? g.lastMessageSequence; return top ? [{ id: g.id, top }] : [] })
const pick = async (rows: Row[]) => {
const shown = (await readable(db, tabId, rows, state)).filter(isMain)
diff --git a/backend/src/superTabs/read/servedTops.ts b/backend/src/superTabs/read/servedTops.ts
new file mode 100644
index 000000000..f8cbda150
--- /dev/null
+++ b/backend/src/superTabs/read/servedTops.ts
@@ -0,0 +1,63 @@
+/**
+ * The newest main row of each project chat as people are served it (#2742). A chat's stored `lastMainSequence` is set when
+ * a row is numbered (parts/commit.ts numberMessages). A `machine-left` line stored before #2742 was numbered for people and
+ * is now read by nobody (model.ts NOTICE_READERS), so a chat whose newest main row is one would stay unread: Desktop and the
+ * phone count a chat unread while that number is past the reader's mark, and send no read at or below the mark the server
+ * holds (Grebe2639's review of #2950). Readers are served instead the newest main row of that chat people read, as the
+ * inbox's latest preview finds it (inbox.ts latestIn): in a first load's whole groups, and in the changed groups a poll sends
+ * whole from the change feed. A poll's other groups are light (parts/light.ts) and carry no such number.
+ *
+ * Only project chats are looked at: those lines were written there alone. Which row is the newest people read depends only on
+ * the stored rows at or below that number, never on the reader (a project chat is no Buddy chat), and stored rows do not
+ * change, so each answer is kept by (Swarm, chat, number) for as long as this server runs, up to KEPT answers; a restarted
+ * server looks each chat up once again. A chat whose number moved is looked up once more: a row people read moves it there,
+ * so that lookup reads the one row at the number by the (tabId, conversationId, sequence) index and answers the number.
+ */
+import type { PrismaClient } from '@prisma/client'
+import type { Conversation, SuperTabState } from '../model.js'
+import { projectsOf } from '../projects.js'
+import { latestIn } from './inbox.js'
+
+/** Answers kept; the one kept longest goes first. */
+export const KEPT = 20_000
+
+export class ServedTops {
+ private readonly kept = new Map<string, number>()
+
+ constructor(private readonly db: PrismaClient, private readonly limit = KEPT) {}
+
+ /** `groups` as readers are served them: each project chat of `state` with the newest main row people read as its
+ * `lastMainSequence`. A first load sends whole groups (its view's); a change read sends the changed ones whole from the
+ * change feed, so a project chat re-sent for a reply in a thread or a reaction is served the same number. */
+ async served<G extends { key: string; data?: unknown }>(tabId: string, state: SuperTabState, groups: G[]): Promise<G[]> {
+ const chats = new Set(projectsOf(state).map(p => p.leadConversationId))
+ const topOf = (g: G) => {
+ const data = g.data as Partial<Conversation> | undefined
+ return chats.has(g.key) && typeof data?.lastMainSequence === 'number' ? data.lastMainSequence : null
+ }
+ const asked = groups.filter(g => topOf(g) !== null)
+ if (!asked.length) return groups
+ const key = (id: string, top: number) => `${tabId}\u0000${id}\u0000${top}`
+ const missing = asked.filter(g => !this.kept.has(key(g.key, topOf(g)!)))
+ if (missing.length) {
+ const chatsAsked = missing.map(g => ({ id: g.key, lastMainSequence: topOf(g)!, lastMessageSequence: (g.data as Conversation).lastMessageSequence }) as Conversation)
+ const { latest } = await latestIn(this.db, tabId, state, chatsAsked)
+ for (const g of missing) {
+ // None read within the rows looked at, or none at all: the stored number stands, as before.
+ const top = topOf(g)!, found = latest.get(g.key)?.sequence
+ this.keep(key(g.key, top), found !== undefined && found < top ? found : top)
+ }
+ }
+ return groups.map(g => {
+ const top = topOf(g)
+ const served = top === null ? null : this.kept.get(key(g.key, top)) ?? top
+ return served === null || served === top ? g : { ...g, data: { ...(g.data as object), lastMainSequence: served } }
+ })
+ }
+
+ private keep(key: string, top: number) {
+ this.kept.delete(key)
+ this.kept.set(key, top)
+ while (this.kept.size > this.limit) this.kept.delete(this.kept.keys().next().value!)
+ }
+}
diff --git a/backend/src/superTabs/read/service.ts b/backend/src/superTabs/read/service.ts
index ed3091fa5..bf2b90b22 100644
--- a/backend/src/superTabs/read/service.ts
+++ b/backend/src/superTabs/read/service.ts
@@ -40,6 +40,7 @@ import { changesFor } from './changes.js'
import { envelope, groupView, readerView, seenIn, type ReaderView } from './views.js'
import { decodeDormantPage, encodeDormantPage } from './dormantPage.js'
import { moveChatMark } from './threadMarkSeeds.js'
+import { ServedTops } from './servedTops.js'
import { memberSwarms } from '../swarmRows.js'
type LoadOptions = NonNullable<Parameters<typeof parts.loadActive>[2]>
@@ -95,8 +96,11 @@ export class SwarmReads {
this.db = store.db
this.now = options.now ?? store.now
this.avatarLinks = store.avatars ? new AvatarLinks(store.avatars, store.avatarPrefix) : null
+ this.servedTops = new ServedTops(store.db)
}
private readonly avatarLinks: AvatarLinks | null
+ /** Each project chat's newest main row people read, as readers are served it (servedTops.ts, #2742). */
+ private readonly servedTops: ServedTops
/** The machine lookups of each Swarm read in the last MACHINE_READS_MS, oldest first, keyed by what they depend on. */
private readonly machineReads = new Map<string, { at: number; read: Promise<MachineReads> }>()
@@ -186,8 +190,11 @@ export class SwarmReads {
...marks.map(mark => mark.conversationId), ...threadMarks.keys(), ...settings.keys()])].filter(id => !held.has(id))
const read = await this.load(tx, cached, user, tabId, asAgentId, t0, {},
async () => withStarts(withGroups(base, await readLightGroups(tx, cached, tabId, named)), extra))
+ const sent = changesFor(read.view, changed, read.actor, options.lightMachines ? { since: from.since } : undefined)
+ // A changed project chat comes whole from the change feed, with its stored newest main row (servedTops.ts).
+ if (!asAgentId) sent.groups = await this.servedTops.served(tabId, read.view.state, sent.groups)
return { format: READ_FORMAT, reload: false, cursor: cursorFor(t0, read.parts.visibilityEpoch), serverTime: t0, lastChangedAt: read.lastChangedAt,
- epoch: read.parts.visibilityEpoch, ...changesFor(read.view, changed, read.actor, options.lightMachines ? { since: from.since } : undefined),
+ epoch: read.parts.visibilityEpoch, ...sent,
derived: { ...await this.withAvatars(read.view, t0), ...readableMarks(read, marks, threadMarks, settings) } }
})
}
@@ -491,10 +498,12 @@ export class SwarmReads {
activityDigest(tx, tabId, actor.userId, t0), remindersDigest(tx, tabId, actor.userId)]) : undefined,
readLastChangedAt(tx, cached, tabId),
])
- return { parts: loaded, actor, lastChangedAt: newest,
- view: readerView(loaded, actor, t0, { teamMachines, otherWorkspaceMachines, machineVersions, teamAccountsIdle: this.store.teamAccountsIdle,
- ...(counts ? { mentionsWaiting: counts[0].count, mentionsDigest: counts[0].digest, decisionsWaiting: counts[1].count,
- decisionsDigest: counts[1].digest, activityDigest: counts[2], remindersDigest: counts[3] } : {}) }, state) }
+ const view = readerView(loaded, actor, t0, { teamMachines, otherWorkspaceMachines, machineVersions, teamAccountsIdle: this.store.teamAccountsIdle,
+ ...(counts ? { mentionsWaiting: counts[0].count, mentionsDigest: counts[0].digest, decisionsWaiting: counts[1].count,
+ decisionsDigest: counts[1].digest, activityDigest: counts[2], remindersDigest: counts[3] } : {}) }, state)
+ // People count a chat unread by its newest main row; agents keep no read marks.
+ if (actor.kind === 'person') view.parts.groups = await this.servedTops.served(tabId, state, view.parts.groups)
+ return { parts: loaded, actor, lastChangedAt: newest, view }
}
/** 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/store.ts b/backend/src/superTabs/store.ts
index 813a9aa7c..b55faaa4c 100644
--- a/backend/src/superTabs/store.ts
+++ b/backend/src/superTabs/store.ts
@@ -503,7 +503,7 @@ const receiptOrder = (m: Pick<SuperTabMessage, 'recipientIds'>) => (a: PageDeliv
}
/** A row as a Buddy reads it, without the machine it names (security reviews of #2115): a lost task's reason, in its text and
- * its source (liveness.ts loseTask), a machine leaving the Swarm (machines.ts removalEnded, commands.ts removeMachine), and
+ * its source (liveness.ts loseTask), a machine leaving the Swarm (commands.ts removeMachine; `machine-left` rows, stored before #2742), and
* the machine a Product Manager started on (leadStart.ts leadJoined; Diego, 10-30) or a removed session is still held on
* (liveness.ts unpurged; review 2 of #2258). */
export function withoutMachineNames(m: SuperTabMessage): SuperTabMessage {
@@ -3328,12 +3328,11 @@ export class SuperTabStore {
const now = this.now()
// A machine that went silent while cleaning takes the fallback before this fresh reading can bring it back: its records
// go, its cleanup waits for this host, as the next sweep would have done (machines.ts expireRemovals).
- const ended = expireRemovals(state, now)
+ expireRemovals(state, now)
const stamp = clock.next(), removedIds: string[] = []
// A reading writes only its machine's part, and an agent's part only when its presence changes (spec); a group's only
- // when a task there is lost or a removal ends.
- const saved = async (lost: MessageDraft[] = []) => {
- const rows = [...ended, ...lost]
+ // when a task there is lost.
+ const saved = async (rows: MessageDraft[] = []) => {
const sequences = numberMessages(state, rows, now)
const touches = judgedMachines(loaded.state, state, rows).filter(t => t.key !== machineId)
await this.endRuns(tx, tabId, loaded.state, state)
diff --git a/cli/src/core/main.ts b/cli/src/core/main.ts
index cabbc99a0..8ff5d05a3 100644
--- a/cli/src/core/main.ts
+++ b/cli/src/core/main.ts
@@ -1772,7 +1772,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {
// The recaps: in this process, or in the edge host (services/recapsProcess.ts), told each turn's lifecycle.
if (outOfProcess.has('recaps')) ports.recaps = recapsLink.port; else serviceHost.start('recaps', inline!.startRecaps, coreApi, RECAPS_FALLBACKS)
// Cleaning up after agents: the harnesses a closed harness started, and those idle half an hour (services/agentCleanup.ts).
- if (outOfProcess.has('agentCleanup')) ports.agentCleanup = agentCleanupLink.port; else serviceHost.start('agentCleanup', (core, given) => inline!.startAgentCleanup(core, given, swarm?.retainedHarnesses), coreApi, AGENT_CLEANUP_FALLBACKS)
+ if (outOfProcess.has('agentCleanup')) ports.agentCleanup = agentCleanupLink.port; else serviceHost.start('agentCleanup', (core, given) => inline!.startAgentCleanup(core, given, swarm ? { retained: swarm.retainedHarnesses, closed: swarm.recordCleanupClose } : undefined), coreApi, AGENT_CLEANUP_FALLBACKS)
if (!outOfProcess.has('shell')) serviceHost.serve('shell', inline!.startShell, coreApi, SHELL_REQUESTS)
// Models: grid access and its pin, the model pictures on agents' frames, the keystroke prewarm, where an
// agent on a grid model sends its inference, and the models requests the apps send (services/models.ts):
diff --git a/cli/src/services/agentCleanup.spec.ts b/cli/src/services/agentCleanup.spec.ts
index 0a9ab1f90..2546b8844 100644
--- a/cli/src/services/agentCleanup.spec.ts
+++ b/cli/src/services/agentCleanup.spec.ts
@@ -111,17 +111,21 @@ it('with the Swarm off, cleans up a harness its saved roster names, as any other
expect(t.closeStarted.mock.calls).toEqual([['orphan', 'now'], ['idle', 'idle'], ['pm', 'idle']])
expect(sessions.map(s => s.agentId)).toEqual(['alive'])
t.cleanup.port.stop()
- // In the core's process too (services/inline.ts): started without the Swarm it keeps no one, and with it, its members.
- for (const [retained, closed] of [[undefined, ['orphan']], [(dataDir: string) => retainedHarnesses(dataDir), []]] as const) {
+ // In the core's process too (services/inline.ts): started without the Swarm it keeps no one and tells no one, and with it,
+ // keeps its members and tells it what it closed.
+ const told: string[] = []
+ const swarm = { retained: (dataDir: string) => retainedHarnesses(dataDir), closed: (_dataDir: string, agentId: string) => { told.push(agentId); return true } }
+ for (const [given, closed] of [[undefined, ['orphan']], [swarm, []], [{ closed: swarm.closed }, ['orphan']]] as const) {
const ports = emptyPorts(), live = [kid('orphan')]
const closeStarted = vi.fn(async (agentId: string) => { live.splice(live.findIndex(s => s.agentId === agentId), 1); return { closed: true as const } })
- startAgentCleanup(fakeCore({ dataDir: dir, agents: { live: vi.fn(() => [...live]), resolve: vi.fn(() => undefined), closeStarted } }), ports, retained)
+ startAgentCleanup(fakeCore({ dataDir: dir, agents: { live: vi.fn(() => [...live]), resolve: vi.fn(() => undefined), closeStarted } }), ports, given)
ports.agentCleanup!.closed('lead')
await vi.waitFor(() => expect(live.map(s => s.agentId)).toEqual(closed.length ? [] : ['orphan']))
await settle()
expect(closeStarted.mock.calls.map(([id]) => id)).toEqual(closed)
ports.agentCleanup!.stop()
}
+ expect(told).toEqual(['orphan'])
})
it('rechecks retention after waiting on activity or another orphan close', async () => {
@@ -147,6 +151,39 @@ it('rechecks retention after waiting on activity or another orphan close', async
cascade.cleanup.port.stop()
})
+// 11-04 on the Mac Studio (task #2744): Tern's last group was archived, so no membership kept it. A task its host receives
+// while the cleanup waits on its activity keeps it; another just as idle with no task still closes.
+it('keeps an agent whose assignment arrives inside the close window, and still closes one idle with no task', async () => {
+ const t = setup([kid('tern'), kid('heron')], { tern: 2 * IDLE_CLOSE_MS, heron: 2 * IDLE_CLOSE_MS })
+ const roster = [{ tabId: 'tab', active: false, agent: { harnessId: 'tern', machineId: 'machine' } }, { tabId: 'tab', active: false, agent: { harnessId: 'heron', machineId: 'machine' } }]
+ retainContributions(dir, 'machine', roster)
+ await t.cleanup.look()
+ t.advance(IDLE_CLOSE_MS)
+ t.lastActivity.mockImplementationOnce(async () => {
+ retainContributions(dir, 'machine', roster, Date.now(), [{ tabId: 'tab', assignmentId: 'work', harnessId: 'tern' }])
+ return NOW - IDLE_CLOSE_MS
+ })
+ await t.cleanup.look()
+ expect(t.closeStarted.mock.calls).toEqual([['heron', 'idle']])
+ t.cleanup.port.stop()
+})
+
+// The review of #2953: the Swarm refuses new work only for a session the cleanup closed, never one stopped for Change agent.
+it('tells the Swarm each harness it closed, idle or orphaned, and none it could not close', async () => {
+ const told: string[] = []
+ const t = setup([kid('idle'), kid('working'), kid('orphan', { parentAgentId: 'gone' })], { idle: 2 * IDLE_CLOSE_MS, working: 2 * IDLE_CLOSE_MS },
+ { closed: (dataDir, agentId) => { expect(dataDir).toBe(dir); told.push(agentId); return agentId !== 'orphan' }, refuse: { working: { error: 'SESSION_NOT_IDLE' } } })
+ await t.cleanup.look()
+ t.advance(IDLE_CLOSE_MS)
+ await t.cleanup.look()
+ await t.cleanup.closeStartedBy('gone')
+ expect(told).toEqual(['idle', 'orphan'])
+ // One the Swarm could not record is said: it is then only listed stopped, and may still be assigned.
+ expect(t.log.mock.calls.filter(([line]) => String(line).includes('could not tell the Swarm'))).toEqual([
+ ['[agent-cleanup] could not tell the Swarm it closed orphan (orphan): a task given to it now is lost.']])
+ t.cleanup.port.stop()
+})
+
describe('closing the harnesses a closed harness started', () => {
it('closes them now, working or not, with one line each, and only those another agent started there', async () => {
// Lead itself is gone: it was closed.
diff --git a/cli/src/services/agentCleanup.ts b/cli/src/services/agentCleanup.ts
index 42c2cc1c4..58ae3ef41 100644
--- a/cli/src/services/agentCleanup.ts
+++ b/cli/src/services/agentCleanup.ts
@@ -59,6 +59,10 @@ export interface AgentCleanupDeps {
/** The Super Tab members the Swarm keeps here (superTabs/retention.ts `retainedHarnesses`, through
* superTabs/inServices.ts): absent while the Swarm is off, when none is kept. */
retained?: (dataDir: string) => ReadonlySet<string>
+ /** Told of each harness this closed (superTabs/cleanupCloses.ts `recordCleanupClose`, through superTabs/inServices.ts):
+ * the Swarm then refuses it new work, as it does not one stopped for Change agent (review of #2953). False when it
+ * could not record it. Absent while the Swarm is off. */
+ closed?: (dataDir: string, agentId: string) => boolean
}
const NONE_RETAINED: ReadonlySet<string> = new Set()
@@ -118,6 +122,9 @@ export function createAgentCleanup(core: CoreApi, deps: AgentCleanupDeps = {}) {
if (retainedHarnesses(core.dataDir).has(s.agentId)) return { error: 'SESSION_RETAINED' }
const result: AgentCloseResult = await (core.agents.closeStarted?.(s.agentId, mode) ?? Promise.resolve({ error: 'UNSUPPORTED' }))
if (result.closed) log(`[agent-cleanup] closed ${label(s)}: ${why}. Its conversation is kept.`)
+ if (result.closed && deps.closed && !deps.closed(core.dataDir, s.agentId)) {
+ log(`[agent-cleanup] could not tell the Swarm it closed ${label(s)}: a task given to it now is lost.`)
+ }
return result
}
const failed = (s: RegisteredSession, why: string, result: AgentCloseResult): void =>
@@ -227,6 +234,7 @@ export function createAgentCleanup(core: CoreApi, deps: AgentCleanupDeps = {}) {
return { port, look, closeStartedBy }
}
-export function startAgentCleanup(core: CoreApi, ports: CorePorts, retained?: AgentCleanupDeps['retained']): void {
- ports.agentCleanup = createAgentCleanup(core, retained ? { retained } : {}).port
+/** [swarm]: the Swarm's side, while it is on (superTabs/inCore.ts, through core/main.ts). */
+export function startAgentCleanup(core: CoreApi, ports: CorePorts, swarm?: Pick<AgentCleanupDeps, 'retained' | 'closed'>): void {
+ ports.agentCleanup = createAgentCleanup(core, swarm ?? {}).port
}
diff --git a/cli/src/services/agentCleanupProcess.spec.ts b/cli/src/services/agentCleanupProcess.spec.ts
index ca75217d0..0e6521235 100644
--- a/cli/src/services/agentCleanupProcess.spec.ts
+++ b/cli/src/services/agentCleanupProcess.spec.ts
@@ -82,12 +82,15 @@ describe('the agent cleanup in its own process', () => {
it('keeps the members the Swarm hands it while it is on, and none without it', () => {
const given: Array<Parameters<typeof createAgentCleanup>[1]> = []
const create = (_core: CoreApi, deps?: Parameters<typeof createAgentCleanup>[1]) => { given.push(deps); return { port: { closed: vi.fn(), typed: vi.fn(), stop: vi.fn() }, look: vi.fn(), closeStartedBy: vi.fn() } }
- const retainedHarnesses = vi.fn(() => new Set(['member']))
+ const retainedHarnesses = vi.fn(() => new Set(['member'])), recordCleanupClose = vi.fn()
const base = { dataDir: dir, socketPath: '/s', machineId: 'm', token: 't', run: () => ({ stop: vi.fn() }), create }
- runAgentCleanupService({ ...base, swarm: { retainedHarnesses } })
+ runAgentCleanupService({ ...base, swarm: { retainedHarnesses, recordCleanupClose } })
runAgentCleanupService(base)
expect(given[0]!.retained).toBe(retainedHarnesses)
+ // And the Swarm is told what it closed (superTabs/cleanupCloses.ts).
+ expect(given[0]!.closed).toBe(recordCleanupClose)
expect(given[1]).not.toHaveProperty('retained')
+ expect(given[1]).not.toHaveProperty('closed')
})
it('stops its looks with the process', () => {
diff --git a/cli/src/services/agentCleanupProcess.ts b/cli/src/services/agentCleanupProcess.ts
index 653b075bd..95ed01ec9 100644
--- a/cli/src/services/agentCleanupProcess.ts
+++ b/cli/src/services/agentCleanupProcess.ts
@@ -23,7 +23,7 @@ export interface AgentCleanupServiceOptions {
/** Swapped in tests. */
create?: typeof createAgentCleanup
/** The Swarm's side of this service, while the Swarm is on (serviceProcess.ts, superTabs/inServices.ts). */
- swarm?: Pick<SwarmServices, 'retainedHarnesses'>
+ swarm?: Pick<SwarmServices, 'retainedHarnesses' | 'recordCleanupClose'>
}
/** The core API the cleanup runs on here: the light services' (processCoreApi.ts), with the two ways it acts. */
@@ -51,7 +51,7 @@ export function runAgentCleanupService(options: AgentCleanupServiceOptions): Ser
const cleanup = (options.create ?? createAgentCleanup)(agentCleanupCoreApi(options.dataDir, () => agents, ask), {
// A core that cannot say leaves none: nothing is closed on a list that may be stale.
refresh: async () => { agents = agentsIn(await ask('live', {}).catch(() => null)) ?? [] },
- ...(options.swarm ? { retained: options.swarm.retainedHarnesses } : {}),
+ ...(options.swarm ? { retained: options.swarm.retainedHarnesses, closed: options.swarm.recordCleanupClose } : {}),
})
const service = (options.run ?? runServiceProcess)({
name: 'agentCleanup',
diff --git a/cli/src/superTabs/cleanupCloses.spec.ts b/cli/src/superTabs/cleanupCloses.spec.ts
new file mode 100644
index 000000000..566c053f0
--- /dev/null
+++ b/cli/src/superTabs/cleanupCloses.spec.ts
@@ -0,0 +1,38 @@
+import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { afterEach, expect, it } from 'vitest'
+import { CLEANUP_CLOSED_KEEP_MS, CLEANUP_CLOSED_MAX, cleanupClosed, cleanupClosesFile, recordCleanupClose } from './cleanupCloses.js'
+
+const roots: string[] = []
+const root = () => { const dir = mkdtempSync(join(tmpdir(), 'cleanup-closes-')); roots.push(dir); return dir }
+afterEach(() => { for (const dir of roots.splice(0)) rmSync(dir, { recursive: true, force: true }) })
+
+it('names the sessions the idle cleanup closed for a while, privately, and forgets them after', () => {
+ const dir = root(), now = 1_000_000
+ expect([...cleanupClosed(dir, now)]).toEqual([])
+ recordCleanupClose(dir, 'tern', now)
+ recordCleanupClose(dir, 'heron', now + 1000)
+ expect([...cleanupClosed(dir, now + 1000)].sort()).toEqual(['heron', 'tern'])
+ expect(statSync(cleanupClosesFile(dir)).mode & 0o777).toBe(0o600)
+ expect([...cleanupClosed(dir, now + CLEANUP_CLOSED_KEEP_MS)]).toEqual(['heron'])
+ // A later close drops what has run out, so the file never grows past what it keeps.
+ recordCleanupClose(dir, 'gannet', now + CLEANUP_CLOSED_KEEP_MS + 1000)
+ expect(Object.keys(JSON.parse(readFileSync(cleanupClosesFile(dir), 'utf8')).closed)).toEqual(['gannet'])
+})
+
+it('keeps the newest, reads a damaged file as none, and says, never throwing, when it cannot write', () => {
+ const dir = root(), now = 1_000_000
+ for (let i = 0; i <= CLEANUP_CLOSED_MAX; i++) recordCleanupClose(dir, `h${i}`, now + i)
+ const kept = cleanupClosed(dir, now + CLEANUP_CLOSED_MAX)
+ expect(kept.size).toBe(CLEANUP_CLOSED_MAX)
+ expect(kept.has('h0')).toBe(false)
+ writeFileSync(cleanupClosesFile(dir), '{not json')
+ expect([...cleanupClosed(dir, now)]).toEqual([])
+ writeFileSync(cleanupClosesFile(dir), JSON.stringify({ closed: { ok: now, '../bad': now, odd: 'x' } }))
+ expect([...cleanupClosed(dir, now)]).toEqual(['ok'])
+ const blocked = join(root(), 'file')
+ writeFileSync(blocked, '')
+ expect(recordCleanupClose(join(blocked, 'data'), 'tern', now)).toBe(false)
+ expect(recordCleanupClose(dir, 'tern', now)).toBe(true)
+})
diff --git a/cli/src/superTabs/cleanupCloses.ts b/cli/src/superTabs/cleanupCloses.ts
new file mode 100644
index 000000000..3da8a099f
--- /dev/null
+++ b/cli/src/superTabs/cleanupCloses.ts
@@ -0,0 +1,49 @@
+/** The sessions this machine's agent cleanup closed (services/agentCleanup.ts `closed`), which its report names apart from
+ * the other stopped ones (machines.ts `closedHarnessIds`). The backend refuses new work only for such a session
+ * (backend superTabs/closedAgents.ts sessionClosed): on 11-04 the cleanup closed Tern on the Mac Studio and the PM assigned
+ * it six seconds later (task #2744), while a session Change agent stopped before its rebind, or one a host mid-restart lists
+ * stopped, still takes work (review of #2953).
+ *
+ * Written by the Swarm's side of the cleanup, in whichever process the cleanup runs, read by the machine's report. A close is
+ * kept CLEANUP_CLOSED_KEEP_MS, long after its agent has left the roster (backend closedAgents.ts, 30 s), and a session that
+ * runs again is never named, whatever this says. */
+import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
+import { dirname, join } from 'node:path'
+import { identifier, object } from './wire.js'
+
+export const CLEANUP_CLOSED_KEEP_MS = 10 * 60_000
+/** At most this many, the newest: one machine closes a few an hour. */
+export const CLEANUP_CLOSED_MAX = 1_000
+export const cleanupClosesFile = (dataDir: string) => join(dataDir, 'super-tabs', 'cleanup-closed.json')
+
+function read(dataDir: string, now: number): Map<string, number> {
+ try {
+ const saved = object(object(JSON.parse(readFileSync(cleanupClosesFile(dataDir), 'utf8'))).closed)
+ return new Map(Object.entries(saved).filter((entry): entry is [string, number] =>
+ identifier(entry[0]) && typeof entry[1] === 'number' && now - entry[1] < CLEANUP_CLOSED_KEEP_MS))
+ } catch { return new Map() }
+}
+
+export function cleanupClosed(dataDir: string, now = Date.now()): Set<string> {
+ return new Set(read(dataDir, now).keys())
+}
+
+/** Records that the cleanup closed [harnessId]; false when it could not, which the cleanup says. It never throws: the close is
+ * done either way, and a session it could not record is only listed stopped, as before this file. */
+export function recordCleanupClose(dataDir: string, harnessId: string, now = Date.now()): boolean {
+ const closed = read(dataDir, now)
+ closed.delete(harnessId)
+ closed.set(harnessId, now)
+ for (const oldest of closed.keys()) {
+ if (closed.size <= CLEANUP_CLOSED_MAX) break
+ closed.delete(oldest)
+ }
+ const file = cleanupClosesFile(dataDir)
+ try {
+ mkdirSync(dirname(file), { recursive: true, mode: 0o700 })
+ // Written whole and renamed: the report never reads half of it.
+ writeFileSync(`${file}.next`, JSON.stringify({ version: 1, closed: Object.fromEntries(closed) }), { mode: 0o600 })
+ renameSync(`${file}.next`, file)
+ return true
+ } catch { return false }
+}
diff --git a/cli/src/superTabs/host.spec.ts b/cli/src/superTabs/host.spec.ts
index d131b4c62..348c58759 100644
--- a/cli/src/superTabs/host.spec.ts
+++ b/cli/src/superTabs/host.spec.ts
@@ -1403,6 +1403,17 @@ describe('a delivery poll or claim that keeps failing', () => {
})
})
+// Task #2744: Tern's last group was archived, so its roster row no longer kept it from the idle cleanup when its next task came.
+it('keeps a harness its poll lists an assignment for, though its groups are archived', async () => {
+ const t = setup()
+ vi.mocked(t.core.account.backend).mockImplementation(async () => ({ status: 200, body: { data: hostForm([], { assignments: [
+ { tabId: 'tab', assignmentId: 'work-1', harnessId: 'tern' }] }) } }))
+ const host = t.open()
+ await host.tick()
+ expect([...retainedHarnesses(t.dir)]).toEqual(['tern'])
+ host.stop()
+})
+
it('confirms a stopped assignment only with fresh native evidence, including after contribution removal', async () => {
const t = setup()
const stopped = vi.fn(async (id: string) => id === 'gone')
diff --git a/cli/src/superTabs/host.ts b/cli/src/superTabs/host.ts
index fef30c183..cd7e34a77 100644
--- a/cli/src/superTabs/host.ts
+++ b/cli/src/superTabs/host.ts
@@ -1053,7 +1053,7 @@ export class SuperTabHost {
private async poll(): Promise<void> {
this.passWait = null
// Each second, unless the server pushes Swarm changes: then when told, and in full each minute (hostFeed.ts due).
- const data = (await this.sources.feed.due()).roster
+ const read = await this.sources.feed.due(), data = read.roster
if (this.stopped) return
// Taken once the roster is in: a push heard from here on is read by the next poll, which follows this one at once.
const pushes = new Set(this.pushes), pushAll = this.pushAll || this.now() >= this.pushUntil
@@ -1063,7 +1063,7 @@ export class SuperTabHost {
// shared member. Membership outlives its task, terminal view and host process.
// Retention only pauses cleanup when it fails; it must never hold up delivery.
try {
- retainContributions(this.core.dataDir, this.machineId, data)
+ retainContributions(this.core.dataDir, this.machineId, data, Date.now(), read.assignments)
this.retentionError = null
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
diff --git a/cli/src/superTabs/inCore.ts b/cli/src/superTabs/inCore.ts
index 6d67771c7..9c521cd08 100644
--- a/cli/src/superTabs/inCore.ts
+++ b/cli/src/superTabs/inCore.ts
@@ -21,6 +21,7 @@ export { SWARM_LAUNCH } from './inCore/launch.js'
export { createConfidentialPurge, isTeamDaemon, purgeRecord, teamAppPolicyFor, teamPurgeReport, withPurgeRetries } from './inCore/teamApps.js'
export { typesWhileBusy } from './inCore/deliveries.js'
export { mayBeBuddy, retainedHarnesses, retainedLeads } from './retention.js'
+export { recordCleanupClose } from './cleanupCloses.js'
// A Product Manager switched from its pane (Change agent) is the one stop of a Lead its close guard and stop guard let through.
export { leadCloseProtected, leadSwitchEnded, leadSwitching, mayBeLead } from './leadSwitch.js'
export { forwardAttachmentUpload } from '../lib/superTabAttachmentProxy.js'
diff --git a/cli/src/superTabs/inServices.ts b/cli/src/superTabs/inServices.ts
index 3e741be4f..df963fc2b 100644
--- a/cli/src/superTabs/inServices.ts
+++ b/cli/src/superTabs/inServices.ts
@@ -4,13 +4,15 @@
* core's when they run there (core/main.ts). Off, none of this is loaded and each runs as it does without the Swarm;
* src/architecture.spec.ts checks that they reach the Swarm's code by no other way.
*
- * - The agent cleanup: the Super Tab members it keeps from both forms of cleanup (services/agentCleanup.ts `retained`).
+ * - The agent cleanup: the Super Tab members it keeps from both forms of cleanup (services/agentCleanup.ts `retained`), and
+ * the sessions it closed, which the machine's report names (`closed`).
* - Share: the observers a Super Tab's roster lets watch a harness (services/sharing.ts `observers`).
* - Change agent: a member's replacement, recorded for its host (services/handoff.ts `replacements`).
* - Models: a Swarm's grid, the APIs and models shared with it, and the check that keeps them there
* (services/models.ts `SwarmModels`).
*/
export { retainedHarnesses } from './retention.js'
+export { recordCleanupClose } from './cleanupCloses.js'
export { observerSource } from './observers.js'
export { recordReplacement } from './replacements.js'
export { syncSwarmGrid } from '../lib/swarmGrid.js'
diff --git a/cli/src/superTabs/machines.spec.ts b/cli/src/superTabs/machines.spec.ts
index 90ab58169..0db5125f9 100644
--- a/cli/src/superTabs/machines.spec.ts
+++ b/cli/src/superTabs/machines.spec.ts
@@ -14,6 +14,7 @@ import { PM_HOMES } from './teamDaemon.js'
import { startAgent } from './start.js'
import { hostForm, swarmReply } from './fixtures/read/index.js'
import { HostFeed } from './hostFeed.js'
+import { recordCleanupClose } from './cleanupCloses.js'
import { SwarmReads } from './swarmReads.js'
import { object } from './wire.js'
import { processCoreApi } from '../services/processCoreApi.js'
@@ -1604,6 +1605,41 @@ describe('contributed machine executor', () => {
expect(lastBody(f)).toMatchObject({ runningHarnessIds: ['running', 'reopened'], stoppedHarnessIds: ['stopped'] })
})
+ // The review of #2953: the backend refuses new work only for a session the idle cleanup closed, never one stopped for
+ // Change agent or a restart, so the report names those apart, and publishes when one is named.
+ it('names the stopped sessions the idle cleanup closed, and publishes when one is added', async () => {
+ const f = fixture(); quiet(f)
+ const dataDir = mkdtempSync(join(tmpdir(), 'swarm-closed-'))
+ onTestFinished(() => rmSync(dataDir, { recursive: true, force: true }))
+ ;(f.core as { dataDir: string }).dataDir = dataDir
+ // One the cleanup closed that runs again (resumed) is not named: only a stopped one is.
+ recordCleanupClose(dataDir, 'running', f.deps.now())
+ await f.machine.tick()
+ expect(lastBody(f)).toMatchObject({ runningHarnessIds: ['running'], stoppedHarnessIds: ['stopped'] })
+ expect(lastBody(f)).not.toHaveProperty('closedHarnessIds')
+ recordCleanupClose(dataDir, 'stopped', f.deps.now())
+ await after(f, 5_000)
+ expect(posts(f)).toHaveLength(2)
+ expect(lastBody(f)).toMatchObject({ stoppedHarnessIds: ['stopped'], closedHarnessIds: ['stopped'] })
+ })
+
+ it('drops the closed sessions alone for a backend that refuses them', async () => {
+ const f = fixture(); quiet(f)
+ const dataDir = mkdtempSync(join(tmpdir(), 'swarm-closed-'))
+ onTestFinished(() => rmSync(dataDir, { recursive: true, force: true }))
+ ;(f.core as { dataDir: string }).dataDir = dataDir
+ recordCleanupClose(dataDir, 'stopped', f.deps.now())
+ const original = f.backend.getMockImplementation()!
+ f.backend.mockImplementation(async (core, method, path, body) => {
+ if (method === 'POST' && (body as Record<string, unknown>)?.closedHarnessIds) throw new SuperTabHostError('VALIDATION_ERROR', 'Unrecognized key: closedHarnessIds', 400)
+ return original(core, method, path, body)
+ })
+ await f.machine.tick()
+ expect(posts(f).map(c => 'closedHarnessIds' in (c[3] as object))).toContain(false)
+ expect(lastBody(f)).toMatchObject({ stoppedHarnessIds: ['stopped'] })
+ expect(lastBody(f)).not.toHaveProperty('closedHarnessIds')
+ })
+
it('reports nothing new from a backend that names no recorded session', async () => {
const f = fixture(); quiet(f)
vi.mocked(f.core.agents.byAgent).mockImplementation(id => ['running', 'reopened'].includes(id) ? { agentId: id } as never : undefined)
diff --git a/cli/src/superTabs/machines.ts b/cli/src/superTabs/machines.ts
index f5f2912ba..768c32fc9 100644
--- a/cli/src/superTabs/machines.ts
+++ b/cli/src/superTabs/machines.ts
@@ -23,6 +23,7 @@ import { teamPrivacyObserver } from '../team/observer.js'
import { teamRelayed } from '../team/relay.js'
import type { PrivacyObservation } from '../team/privacy.js'
import { repositoryKey } from './route.js'
+import { cleanupClosed } from './cleanupCloses.js'
import { listClaudeProfiles, codexReportLabel, profileId } from '../lib/claudeProfiles.js'
import { defaultCodexHome, listCodexProfiles } from '../lib/codexProfiles.js'
import { usageTarget, validModelTokens, validUsageTarget, type ModelTokens } from '../lib/agentUsageWire.js'
@@ -256,6 +257,7 @@ export class ContributedMachines {
private progressRefusedUntil = 0
private tokensRefusedUntil = 0
private uncommittedRefusedUntil = 0
+ private closedRefusedUntil = 0
/** Per running session, whether its folder had uncommitted changes when last read, and until when that stands. */
private readonly uncommittedCache = new Map<string, { cwd: string; dirty: boolean; until: number; readAt: number; shown?: number }>()
/** Whether git is reading session folders beside the pass now. */
@@ -508,6 +510,8 @@ export class ContributedMachines {
// Every Swarm's reading first, sent in one call (DB roadmap 2.5), then each Swarm's starts.
const looked: Array<{ row: Record<string, unknown>; pool: Record<string, unknown>; pending: ReturnType<typeof startsOf> }> = []
const due: Reading[] = []
+ // The sessions the idle cleanup closed, read once a pass: only those the backend refuses work (review of #2953).
+ const cleaned = began >= this.closedRefusedUntil ? cleanupClosed(this.core.dataDir, began) : new Set<string>()
for (const row of pools) {
try {
if (this.stopped || !identifier(row.tabId)) return
@@ -529,6 +533,7 @@ export class ContributedMachines {
if (!ids.includes(id) && this.core.agents.byAgent(id) && !await this.core.agents.stopped?.(id)) reopened.push(id)
}
const running = [...ids.filter(id => !!this.core.agents.byAgent(id) && !stopped.includes(id)), ...reopened]
+ const closed = stopped.filter(id => cleaned.has(id))
const pending = startsOf(row)
looked.push({ row, pool, pending })
const preparing = began >= this.preparingRefusedUntil ? pending.flatMap(start => this.preparing.has(String(start.id)) ? [String(start.id)] : []) : []
@@ -540,7 +545,7 @@ export class ContributedMachines {
const wanted = object(object(start.request).requirements).tools
return Array.isArray(wanted) ? wanted.filter((name): name is string => typeof name === 'string' && /^[A-Za-z0-9_.+-]{1,80}$/.test(name)) : []
})])].slice(0, 20)
- const signature = JSON.stringify([row.reservations, pool.generation, [...running].sort(), [...stopped].sort(), row.paused === true || pool.paused === true,
+ const signature = JSON.stringify([row.reservations, pool.generation, [...running].sort(), [...stopped].sort(), [...closed].sort(), row.paused === true || pool.paused === true,
pending.map(start => start.id).sort(), [...preparing].sort(), refusedStarts.map(r => r.startId).sort(),
// A privacy result that changes (a home folder opened again) is sent at once, not on the next beat.
privacy ? [privacy.mode, 'passed' in privacy ? privacy.passed : null, 'item' in privacy ? privacy.item : null] : null,
@@ -629,6 +634,7 @@ export class ContributedMachines {
chip: typeof resources.chip === 'string' ? resources.chip : null, os: typeof resources.os === 'string' ? resources.os : null,
gpuPercent: gpus.length ? Math.max(...gpus) : null },
quotas: allowances.map(q => ({ engine: q.engine, known: q.known, windows: q.windows })), runningHarnessIds: running, stoppedHarnessIds: stopped,
+ ...(closed.length ? { closedHarnessIds: closed } : {}),
...(Array.isArray(row.reservations) ? { stoppedReservations: list(row.reservations).filter(r => stopped.includes(String(r.harnessId))
&& identifier(r.harnessId) && identifier(r.generation)).map(r => ({ harnessId: r.harnessId, generation: r.generation })) } : {}) }
due.push({ tab: String(row.tabId), observation, localModels, signature })
@@ -848,7 +854,8 @@ export class ContributedMachines {
*/
private async sendAlone({ tab, observation, localModels }: Reading, began: number, refused?: SuperTabHostError): Promise<void> {
const path = `/api/super-tabs/${tab}/machines/${this.machineId}`
- const { refusedStarts: _refusedStarts, ...unrefused } = observation
+ const { closedHarnessIds: _closed, ...unclosed } = observation
+ const { refusedStarts: _refusedStarts, ...unrefused } = unclosed
const { profiles: _profiles, ...unprofiled } = unrefused
const { quotaReasons: _reasons, ...unreasoned } = unprofiled
const { harnessTokens: _tokens, ...untokened } = unreasoned
@@ -861,13 +868,14 @@ export class ContributedMachines {
// A backend before `starting` refuses it: it gets the models the grid lists, as before, not none at all.
const listedOnly = localModels?.some(m => m.starting)
? [{ ...withoutPrivacy, localModels: localModels.filter(m => !m.starting) }] : []
- const attempts = [...('refusedStarts' in observation ? [observation] : []), ...('profiles' in unrefused ? [unrefused] : []), ...('quotaReasons' in unprofiled ? [unprofiled] : []), ...('harnessTokens' in unreasoned ? [unreasoned] : []), ...('preparingStartIds' in untokened ? [untokened] : []),
+ const attempts = [...('closedHarnessIds' in observation ? [observation] : []), ...('refusedStarts' in unclosed ? [unclosed] : []), ...('profiles' in unrefused ? [unrefused] : []), ...('quotaReasons' in unprofiled ? [unprofiled] : []), ...('harnessTokens' in unreasoned ? [unreasoned] : []), ...('preparingStartIds' in untokened ? [untokened] : []),
...('harnessUncommitted' in unprepared ? [unprepared] : []),
...('readiness' in known ? [known] : []), ready, ...('harnessProgress' in ready ? [withoutProgress] : []),
...('privacy' in withoutProgress ? [withoutPrivacy] : []), ...listedOnly,
...('toolProbeErrors' in observation || 'localModels' in observation || 'refusedModels' in observation ? [legacy] : [])]
const refuse = (attempt: Record<string, unknown>) => {
- if ('refusedStarts' in attempt) this.refusedStartsRefusedUntil = began + FIELD_REFUSED_MS
+ if ('closedHarnessIds' in attempt) this.closedRefusedUntil = began + FIELD_REFUSED_MS
+ else if ('refusedStarts' in attempt) this.refusedStartsRefusedUntil = began + FIELD_REFUSED_MS
else if ('profiles' in attempt) this.profilesRefusedUntil = began + FIELD_REFUSED_MS
else if ('quotaReasons' in attempt) this.quotaReasonsRefusedUntil = began + FIELD_REFUSED_MS
else if ('harnessTokens' in attempt) this.tokensRefusedUntil = began + FIELD_REFUSED_MS
diff --git a/cli/src/superTabs/retention.spec.ts b/cli/src/superTabs/retention.spec.ts
index 170f7716b..028b9846f 100644
--- a/cli/src/superTabs/retention.spec.ts
+++ b/cli/src/superTabs/retention.spec.ts
@@ -2,7 +2,7 @@ import { mkdtempSync, readFileSync, rmSync, statSync, unlinkSync, utimesSync, wr
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, expect, it } from 'vitest'
-import { mayBeBuddy, mayBeLead, RETENTION_LAPSE_MS, retainContributions, retainedHarnesses, retainedLeads, retentionFile } from './retention.js'
+import { ASSIGNED_GRACE_MS, mayBeBuddy, mayBeLead, RETENTION_LAPSE_MS, retainContributions, retainedHarnesses, retainedLeads, retentionFile } from './retention.js'
const roots: string[] = []
const root = () => { const dir = mkdtempSync(join(tmpdir(), 'super-retention-')); roots.push(dir); return dir }
@@ -161,3 +161,25 @@ it('lets a Buddy lapse with a roster nobody confirms for a week', () => {
expect(mayBeBuddy(dir, 'buddy', now + RETENTION_LAPSE_MS - 1000)).toBe(true)
expect(mayBeBuddy(dir, 'buddy', now + RETENTION_LAPSE_MS + 1000)).toBe(false)
})
+
+// 11-04 on the Mac Studio: Tern's last group was archived, so the roster no longer kept it, and the idle cleanup may close it
+// in the very poll its next task arrives in (task #2744). An assignment the host holds keeps its harness, and for a minute.
+it('retains a harness that holds an assignment, and for a minute after the host first received it', () => {
+ const dir = root(), now = Date.now(), file = retentionFile(dir)
+ const work = (harnessId: string) => ({ tabId: 'tab', assignmentId: `work-${harnessId}`, harnessId })
+ retainContributions(dir, 'machine', [row('tern', { active: false })], now, [work('tern'), { tabId: 'tab' }, work('../bad')])
+ expect([...retainedHarnesses(dir, now)]).toEqual(['tern'])
+ // Unchanged while it holds it: no write each poll.
+ const before = statSync(file, { bigint: true }).mtimeNs
+ retainContributions(dir, 'machine', [row('tern', { active: false })], now + 20_000, [work('tern')])
+ expect(statSync(file, { bigint: true }).mtimeNs).toBe(before)
+ // Its assignment ended: kept until a minute after it first came, then released.
+ retainContributions(dir, 'machine', [row('tern', { active: false })], now + ASSIGNED_GRACE_MS - 1, [])
+ expect([...retainedHarnesses(dir, now + ASSIGNED_GRACE_MS - 1)]).toEqual(['tern'])
+ retainContributions(dir, 'machine', [row('tern', { active: false })], now + ASSIGNED_GRACE_MS, [])
+ expect([...retainedHarnesses(dir, now + ASSIGNED_GRACE_MS)]).toEqual([])
+ // A roster read alone, as an older caller makes it, keeps the minute of one received just now.
+ retainContributions(dir, 'machine', [], now, [work('heron')])
+ retainContributions(dir, 'machine', [], now + 1000)
+ expect([...retainedHarnesses(dir, now + 1000)]).toEqual(['heron'])
+})
diff --git a/cli/src/superTabs/retention.ts b/cli/src/superTabs/retention.ts
index dd69389c4..2270212fd 100644
--- a/cli/src/superTabs/retention.ts
+++ b/cli/src/superTabs/retention.ts
@@ -13,11 +13,16 @@ import { identifier, object } from './wire.js'
/** `leads`: the Leads of active projects here, which never lapse (owner, 2026-10-23: the Lead of an active project is never
* stopped in any case). Absent in a file from before Leads were kept.
* `buddies`: the Buddies here, which a close never stops (Diego, 10-30): only their person removing them, or the machine's
- * owner with Stop Harness. Absent in a file from before Buddies were kept, which names none until the next roster. */
-interface Retention { version: 2; machineId: string; harnesses: string[]; leads?: string[]; buddies?: string[] }
+ * owner with Stop Harness. Absent in a file from before Buddies were kept, which names none until the next roster.
+ * `assigned`: when the host first received each assignment its harnesses hold, epoch ms. Such a harness is in `harnesses`
+ * while it holds it and for ASSIGNED_GRACE_MS after it came, whatever its groups (task #2744). Absent in older files. */
+interface Retention { version: 2; machineId: string; harnesses: string[]; leads?: string[]; buddies?: string[]; assigned?: Record<string, number> }
export const retentionFile = (dataDir: string) => join(dataDir, 'super-tabs', 'retained.json')
export const RETENTION_LAPSE_MS = 7 * 24 * 60 * 60_000
const REFRESH_MS = 60 * 60_000
+/** How long a harness stays kept after the host first received an assignment for it: an assignment that ends at once (the
+ * PM moves it, the agent answers in a second) never leaves the harness to a cleanup that was about to close it. */
+export const ASSIGNED_GRACE_MS = 60_000
function read(file: string): Retention {
if (statSync(file).size > 4 * 1024 * 1024) throw new Error('Super Tab retention is too large. Automatic cleanup is paused.')
@@ -25,7 +30,8 @@ function read(file: string): Retention {
try { value = object(JSON.parse(readFileSync(file, 'utf8'))) } catch { /* reported below */ }
if (value.version !== 2 || !identifier(value.machineId) || !Array.isArray(value.harnesses) || !value.harnesses.every(identifier)
|| (value.leads !== undefined && (!Array.isArray(value.leads) || !value.leads.every(identifier)))
- || (value.buddies !== undefined && (!Array.isArray(value.buddies) || !value.buddies.every(identifier)))) {
+ || (value.buddies !== undefined && (!Array.isArray(value.buddies) || !value.buddies.every(identifier)))
+ || (value.assigned !== undefined && !Object.entries(object(value.assigned)).every(([id, at]) => identifier(id) && Number.isFinite(at)))) {
throw new Error('Super Tab retention is unreadable. Automatic cleanup is paused.')
}
return value as unknown as Retention
@@ -68,7 +74,9 @@ export function mayBeBuddy(dataDir: string, agentId: string, now = Date.now()):
try { return (read(file).buddies ?? []).includes(agentId) } catch { return true }
}
-export function retainContributions(dataDir: string, machineId: string, rows: unknown[], now = Date.now()): void {
+/** [assignments]: the host form's assignment rows for this machine, which hold their harnesses (task #2744: Tern's last group
+ * was archived, so membership alone no longer kept it). */
+export function retainContributions(dataDir: string, machineId: string, rows: unknown[], now = Date.now(), assignments: unknown[] = []): void {
if (!identifier(machineId)) throw new Error('Invalid Super Tab retention machine.')
const file = retentionFile(dataDir)
// This machine's confirmed roster is authoritative: it replaces an unreadable
@@ -92,7 +100,16 @@ export function retainContributions(dataDir: string, machineId: string, rows: un
else if (row.active === false && !buddies.has(agent.harnessId)) continue
ids.add(agent.harnessId)
}
- const next: Retention = { version: 2, machineId, harnesses: [...ids].sort(), leads: [...leads].sort(), buddies: [...buddies].sort() }
+ const assigned: Record<string, number> = {}
+ const before = previous?.machineId === machineId ? previous.assigned ?? {} : {}
+ for (const item of assignments) {
+ const row = object(item)
+ if (identifier(row.tabId) && identifier(row.harnessId)) assigned[row.harnessId] = before[row.harnessId] ?? now
+ }
+ for (const [id, at] of Object.entries(before)) if (!(id in assigned) && now - at < ASSIGNED_GRACE_MS) assigned[id] = at
+ for (const id of Object.keys(assigned)) ids.add(id)
+ const next: Retention = { version: 2, machineId, harnesses: [...ids].sort(), leads: [...leads].sort(), buddies: [...buddies].sort(),
+ assigned: Object.fromEntries(Object.entries(assigned).sort(([a], [b]) => a.localeCompare(b))) }
if (previous && JSON.stringify(previous) === JSON.stringify(next)) {
if (now - statSync(file).mtimeMs > REFRESH_MS) utimesSync(file, new Date(now), new Date(now))
return
diff --git a/cli/src/superTabs/routeCommand.spec.ts b/cli/src/superTabs/routeCommand.spec.ts
index e8aef9d59..12bba2109 100644
--- a/cli/src/superTabs/routeCommand.spec.ts
+++ b/cli/src/superTabs/routeCommand.spec.ts
@@ -1180,6 +1180,17 @@ describe('routing contributed capacity', () => {
expect(recorded(s).command.decision.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('Multiply'),
reason: 'Its terminal is not running.' })]))
})
+ // Task #2744: the idle cleanup closed Tern, and the PM assigned it anyway, over the route's pick; the reason says what to do.
+ // Only for one its idle cleanup closed: one listed stopped alone may be a Change agent before its rebind (review of #2953).
+ it('says a contributed member its idle cleanup closed was closed, and to start a new agent', async () => {
+ for (const [closed, reason] of [[['h1'], 'Its session was closed. Start a new agent.'], [[], 'Its terminal is not running.']] as const) {
+ const pool = healthy(); pool.observation.runningHarnessIds = []
+ Object.assign(pool.observation, { stoppedHarnessIds: ['h1'], closedHarnessIds: closed })
+ const { answer, s } = await runPool(pool, true)
+ expect(answer).toMatchObject({ ok: true, pick: { kind: 'new' } })
+ expect(recorded(s).command.decision.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('Multiply'), reason })]))
+ }
+ })
it('keeps a contributed member with no running-terminal list pickable and warns that its terminal is unverified', async () => {
const pool = healthy(); delete (pool.observation as Partial<typeof pool.observation>).runningHarnessIds
const { answer, s } = await runPool(pool, true)
diff --git a/cli/src/superTabs/routeCommand.ts b/cli/src/superTabs/routeCommand.ts
index d55ba2a98..64646dbc4 100644
--- a/cli/src/superTabs/routeCommand.ts
+++ b/cli/src/superTabs/routeCommand.ts
@@ -143,9 +143,12 @@ export function relayedReadings(pool: Json, pools: Json[], now = Date.now()): {
}
/** A running member keeps its engine and memory, but still needs a live terminal and the task's tools and devices. Its
* allowance is information ([usedUp]). */
-function memberRefusals(running: boolean | null, tools: string[], devices: string[], reportedTools: Json, reportedDevices: Json, toolProbeErrors: Json): string[] {
+function memberRefusals(running: boolean | null, closed: boolean, tools: string[], devices: string[], reportedTools: Json, reportedDevices: Json, toolProbeErrors: Json): string[] {
return [
- ...(running === false ? ['Its terminal is not running.'] : []),
+ // A session its machine's idle cleanup closed never comes back as this agent: the backend refuses to assign it (superTabs/
+ // closedAgents.ts sessionClosed), so the reason says what to do (task #2744). One listed stopped alone may be a Change
+ // agent before its rebind (review of #2953).
+ ...(running === false ? [closed ? 'Its session was closed. Start a new agent.' : 'Its terminal is not running.'] : []),
...tools.map(tool => requiredToolReason(tool, reportedTools[tool], toolProbeErrors[tool])).filter((reason): reason is string => reason !== null),
...devices.filter(device => reportedDevices[device] !== true).map(device => `Required device unavailable: ${device}.`),
]
@@ -632,7 +635,8 @@ async function probeMachine(machineId: string, { name: machineName, status, acco
return [...members.map(m => {
const engine = String(object(m.configuration).engine), allowance = quota(engine)
const running = Array.isArray(observed.runningHarnessIds) ? observed.runningHarnessIds.includes(m.harnessId) : null
- return { ...memberProbe(m, fresh ? [...memberRefusals(running, needs.tools, needs.devices, tools, object(observed.devices), object(observed.toolProbeErrors)),
+ const closed = Array.isArray(observed.closedHarnessIds) && observed.closedHarnessIds.includes(m.harnessId)
+ return { ...memberProbe(m, fresh ? [...memberRefusals(running, closed, needs.tools, needs.devices, tools, object(observed.devices), object(observed.toolProbeErrors)),
...kept, ...teammates(m.contributorId)] : [OFFLINE],
allowance, null, fresh && running === null, [...pushNote, ...usedUp(allowance)]), ...(fresh ? { load } : {}) }
}),
@@ -662,7 +666,7 @@ async function probeMachine(machineId: string, { name: machineName, status, acco
const inspected = byEngine.get(String(object(m.configuration).engine))!
const allowance = quotaOf2(inspected.quota)
const capacity = object(inspected.capacity)
- const refusals = memberRefusals(live.has(String(m.harnessId)), needs.tools, needs.devices,
+ const refusals = memberRefusals(live.has(String(m.harnessId)), false, needs.tools, needs.devices,
object(capacity.tools), object(capacity.devices), object(capacity.toolProbeErrors))
const frame = frames.find(f => f.id === m.harnessId)
probes.push({ ...memberProbe(m, refusals, allowance, frame ? frameOf(frame) : null, false, usedUp(allowance)),
diff --git a/docs/plans/2026-11-04-idle-cleanup-closed-agents-plan.md b/docs/plans/2026-11-04-idle-cleanup-closed-agents-plan.md
new file mode 100644
index 000000000..ba7515d3e
--- /dev/null
+++ b/docs/plans/2026-11-04-idle-cleanup-closed-agents-plan.md
@@ -0,0 +1,100 @@
+# Idle cleanup and closed Swarm agents: plan
+
+> Written by the task's Engineer and implemented by it, plan task by plan task, test first (project instructions,
+> Engineer). No Execution Handoff.
+
+**Goal:** A Swarm agent is never given a task after the idle cleanup closed its session, and the cleanup never closes an
+agent that holds an assignment, while genuinely idle agents still close after 30 minutes (task #2744).
+
+**Architecture:** Two halves of one race. The backend refuses `work.assign` to an agent whose machine's fresh reading
+proves its session stopped (`AGENT_CLOSED`), so the PM's assign can no longer land on a closed session; the existing
+removal (`closedAgents.ts`, 30 s after the stopped reading) then takes the agent off the roster with no task and no
+"lost" line. The CLI host keeps every harness that holds an assignment in the Super Tab retention file, and for a minute
+after the host first received it, so the cleanup's re-read just before a close (already there) also sees work that
+arrived after the agent's last group was archived.
+
+**Tech Stack:** TypeScript; backend (Fastify, Prisma/Mongo state parts, vitest); CLI daemon services and Swarm host
+(vitest).
+
+**Spec:** the brief of task #2744. What the logs showed, 11-04 on the Mac Studio (`~/.harness/cli/data/harness.log`
+and the task's chat): the cleanup closed Tern at 13:57:51 VN, and the PM assigned Tern at 13:57:57, six seconds *after*
+the close. Tern's last group was archived, so it was no member the retention file kept. The machine's next reading (every
+5 s) listed the session stopped, but `work.assign` does not read it, so the task went to a closed session and was lost;
+an agent holding a lost task is kept on the roster until its Lead hands the task on (`removeClosedAgents`), which is why
+it stayed listed until 14:10.
+
+## Global Constraints
+
+- Swarm code stays out of `cli/src/core`. The cleanup (`cli/src/services/agentCleanup.ts`) keeps reading retention
+ through its `retained` port; nothing Swarm-specific goes into it.
+- No new stored data in the backend: the refusal reads the machine's last observation, which is already stored.
+- The retention file stays version 2 and is only extended (`assigned`, optional). An older CLI reading a newer file
+ ignores the field; a newer CLI reading an older file sees no assignments, as before.
+- Old clients: an older CLI's assign of a closed agent gets the same refusal with a message the PM can act on.
+- Red-first: every risk the brief names gets a test that fails without its change.
+
+## Review Focus
+
+- The refusal must never hit a live session: a session resumed after a stop (renewed reservation without generation
+ proof), a stale reading (machine silent), or a reading that lists it running.
+- The `assigned` grace must not keep anything forever: an entry goes once its assignment row is gone and a minute has
+ passed since it was first received.
+
+## Design
+
+- growth: `assigned` holds at most the machine's assignment rows plus those ended in the last minute; an entry goes on
+ the next write after both are past.
+- concurrency: the retention file is written whole and renamed by the host's poll, as today; the backend check runs
+ inside the command's state transaction.
+- read cost: the host passes the assignment rows it already read; no new request. The backend reads one machine record.
+- scale: 10x agents per machine is 10x entries of ~60 bytes; the 4 MB file cap is far away.
+- compatibility: covered above (file extended, refusal is a new error code on an existing command).
+- migration + rollback: none; rolling back leaves an `assigned` field older CLIs ignore.
+- audience: unchanged; the retention file is 0600 in the daemon's data folder.
+- identity: N/A because nothing here signs in or acts as anyone.
+- failure: the refusal says why and what to do ("start a new agent"); a retention write failure is logged and pauses
+ cleanup, as today.
+- platforms: macOS, Linux and Windows CLI (same code); backend.
+
+## Task 1: Backend refuses assigning a closed session
+
+**Files:** `backend/src/superTabs/closedAgents.ts` (`sessionStopped`), `backend/src/superTabs/commands.ts`
+(`work.assign`), `backend/src/superTabs/closedAgents.test.ts`, `backend/src/superTabs/commands.test.ts`.
+
+**Interfaces:** `sessionStopped(state, target, now): boolean` — the agent's machine's last reading is fresh
+(`MACHINE_FRESH_MS`), lists its harness stopped and not running, with the same reservation proof `closedAgents` uses.
+
+- [x] Test: an agent whose fresh reading lists it stopped is refused with `AGENT_CLOSED` and nothing changes (no
+ assignment, no member added); one on a stale reading, one listed running, and a renewed reservation without proof are
+ assigned as before. Watch it fail.
+- [x] Code: `sessionStopped` and the check in `work.assign` before any change. Watch it pass.
+- [x] Test: a closed agent the assign was refused for leaves the roster on the next report with no lost line (existing
+ removal, now reachable). Run `closedAgents.test.ts` and the `work.assign` tests.
+
+## Task 2: CLI retention keeps harnesses that hold an assignment
+
+**Files:** `cli/src/superTabs/retention.ts` (`retainContributions`, `retainedHarnesses`), `cli/src/superTabs/host.ts`
+(poll passes its assignment rows), `cli/src/superTabs/retention.spec.ts`, `cli/src/services/agentCleanup.spec.ts`.
+
+**Interfaces:** `retainContributions(dataDir, machineId, rows, now, assignments = [])`; the file's optional
+`assigned: Record<harnessId, firstReceivedAtMs>`; `ASSIGNED_GRACE_MS = 60_000`.
+
+- [x] Test (retention): a harness whose roster row is inactive (only archived groups) but which holds an assignment row
+ is retained; after the row is gone it stays retained until a minute after it was first received, then is released;
+ an unchanged assignment set does not rewrite the file. Watch it fail.
+- [x] Test (cleanup): an assignment arriving inside the close window (between the look's read and the close) keeps the
+ agent; a long-idle agent with no task is still closed. Watch the first fail.
+- [x] Code: record and read `assigned`; the host's poll passes `assignments`. Watch them pass.
+- [x] Run `retention.spec.ts`, `agentCleanup.spec.ts`, `host.spec.ts`, and `npm run test:core` (services gate).
+
+## Task 3: The route says why a closed member is left out
+
+Asked by the PM in the task's thread (11-04): the PM's tools and the route skip a closed agent, not just fail on assign.
+The route already left out a member its machine's fresh reading does not list running; its reason now says what to do
+when the machine proved the session stopped.
+
+**Files:** `cli/src/superTabs/routeCommand.ts` (`memberRefusals`), `cli/src/superTabs/routeCommand.spec.ts`.
+
+- [x] Test: a contributed member listed stopped is excluded with "Its session was closed. Start a new agent." Watch it
+ fail (it said "Its terminal is not running.").
+- [x] Code: `memberRefusals` takes whether the reading lists it stopped. Watch it pass.
diff --git a/docs/plans/2026-11-04-no-machine-left-lines-plan.md b/docs/plans/2026-11-04-no-machine-left-lines-plan.md
new file mode 100644
index 000000000..d3aca0184
--- /dev/null
+++ b/docs/plans/2026-11-04-no-machine-left-lines-plan.md
@@ -0,0 +1,88 @@
+# No "machine left the Swarm" lines in chats: plan
+
+> Way of working: the project's Engineer role. Plan first, then each task test first (writing-plans, test-driven-development).
+
+**Goal:** anh Dee (11-04, #lamp-gtm on the phone): "remove these messages". These are the lines
+"<machine> left the Swarm: its host confirmed its cleanup." and "... it is offline. It cleans up when it next connects.",
+posted in every project chat as the machine's owner. New ones are no longer written. The ones already stored disappear from every
+chat, preview and unread count.
+
+**Architecture:** all on the backend. `machines.ts removalEnded` writes the lines (source kind `machine-left`). It goes away,
+and removals end exactly as before, without a line. Every read decides per row kind who may see a stored row, at read time
+(`model.ts NOTICE_READERS` through `agentOnly`: pages, the inbox's latest preview, Activity, mentions, deliveries). With
+`machine-left` read by nobody (`agent`; these rows have no recipients), the stored rows are hidden from Desktop, web and
+every phone build already installed, with no app release. One catch: a project chat whose newest main row is such a line keeps
+`lastMainSequence` at it, and Desktop and the phone count a chat unread while that number is past the reader's mark. They
+also send no read at or below the mark the server already holds, so a read-time fix on the mark never runs (review 1,
+Grebe2639). The server therefore serves each project chat's `lastMainSequence` as the newest main row people read
+(`read/servedTops.ts`, using the inbox's own lookup `inbox.ts latestIn`), with no stored data changed.
+
+**Tech Stack:** TypeScript backend (vitest unit tests, MongoDB integration tests).
+
+**Spec:** task #2742 brief. One change from it, said in the task's chat: old lines are hidden by the backend rather than by
+each app, so no app code changes. The `machine-removed` rows ("<machine> left the Swarm with its owner, and "X" with it. Its
+task has no agent now.") stay. They go only to the groups whose agents went, and say which task lost its agent.
+
+## Global Constraints
+
+- Nothing else about machine removal changes: cleanup records, the 30-minute fallback, offline removal, Team accounts.
+- Read cost (ENGINEERING.md section 4): a person's first load and a poll's changed project chats are looked up, each
+ (Swarm, chat, `lastMainSequence`) once per server process (kept up to 20,000 answers). A poll's other groups are light
+ and carry no number. A lookup is the inbox's existing one: the row at that number by the (tabId, conversationId,
+ sequence) index, and at most `LATEST_SCAN` rows more only when that row is hidden. Measured on the bench Swarm (two runs
+ each way, 15 polls each, medians): idle polls and polls after a task-group post unchanged (62,012 database bytes,
+ 17 to 21 ms); a poll after a project-chat post +674 database bytes and about +1.5 ms (18.4/18.8 to 20.4/19.9 ms).
+- No data migration: stored rows stay, hidden at read time. Rollback: an older backend shows them again.
+
+## Review Focus
+
+- That no reader, an agent included, still gets a stored `machine-left` row (pages, inbox, Activity, deliveries).
+- That the served `lastMainSequence` is never past the stored one, and never below a main row people read.
+
+## Design
+
+- growth: no new data; the lines are no longer written.
+- concurrency: N/A because nothing new is written; the kept answers are per process and immutable per key.
+- read cost: see Global Constraints.
+- scale: lookups grow with project-chat messages (one per new number per process), not with reads; the warning metric is read latency of first loads.
+- compatibility: older apps get the same pages, without these rows; nothing in the API changes.
+- migration + rollback: none / an older backend shows the stored rows again.
+- audience: the server enforces it (read-time `agentOnly`).
+- identity: N/A because nothing is written as anyone.
+- failure: N/A because nothing new can fail; a removal still ends.
+- platforms: backend only, so every client gets it.
+
+## Task 1: removals end without a line
+
+**Files:** `backend/src/superTabs/machines.ts`, `backend/src/superTabs/commands.ts`, `backend/src/superTabs/store.ts`,
+`backend/src/superTabs/commands.test.ts`, integration tests that read the lines (`superTabTeam`, `superTabBuddyProbes`).
+
+**Interfaces:** `expireRemovals(state, now): void` (was `MessageDraft[]`); `removalEnded` removed.
+
+- [ ] Change the unit tests of confirmed, offline and 30-minute-fallback removals to expect no `machine-left` line. Run
+ them and see them fail.
+- [ ] Remove `removalEnded` and its calls. Run the tests and see them pass.
+
+## Task 2: stored lines are read by nobody, and a mark reads through them
+
+**Files:** `backend/src/superTabs/model.ts`, `backend/src/superTabs/read/service.ts`, `backend/integration/superTabInbox.test.ts`,
+`backend/integration/superTabBuddies.test.ts`.
+
+- [ ] Integration test: a stored `machine-left` line is missing from the chat's page and from the inbox's latest preview. Run it and see it fail.
+- [ ] `NOTICE_READERS['machine-left'] = 'agent'`. Run it and see it pass.
+
+## Task 3 (review 1): a caught-up person's chat reads as read with no read sent
+
+**Files:** `backend/src/superTabs/read/servedTops.ts`, `backend/src/superTabs/read/service.ts`, `backend/src/superTabs/read/inbox.ts`,
+`backend/integration/superTabInbox.test.ts`.
+
+- [ ] Integration test: a person reads the project chat, then a machine-left line is stored. Their first load serves the
+ chat's newest main row at their mark, with no read sent. Run it and see it fail.
+- [ ] Serve each project chat's newest main row people read (`ServedTops`); drop the mark read-through it replaces. Run it
+ and see it pass.
+
+## Task 4 (review 2): a poll's changed project chat is served the same number
+
+- [ ] Integration test: after the line, a reply only in a thread re-sends the project chat in a poll; its
+ `lastMainSequence` is the newest row people read. Run it without the change-read path and see it fail.
+- [ ] Apply `ServedTops` to a change read's groups too. Measure a poll before and after (Read cost above).