The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
Count every machine's slots from one pass over the Swarm's lists (change reads ~32% less CPU) (#2392)
0xken158ebf44ea7b82
diff --git a/backend/src/superTabs/leadStart.ts b/backend/src/superTabs/leadStart.tsindex f347e76e4..a84bc04ab 100644--- a/backend/src/superTabs/leadStart.ts+++ b/backend/src/superTabs/leadStart.ts@@ -5,7 +5,7 @@* names the machine: only they know which one stays on (Ken, 10-29). Desktop's own-machine flow (project.create, a local* start, agent.add) is unchanged. */import { admitMachineStart, contributedEngines, failTeamWork, machineContribution, projectFolder, projectsOnMachine, teamAccountPool, teamAccountSwarm, teamPoolOf,- teamWorkRefusal, type ContributedMachine } from './machines.js'+ slotCounter, teamWorkRefusal, type ContributedMachine } from './machines.js'import { actorKey, fail, person, SuperTabError, type AgentStart, type AgentStartRequest, type Conversation, type Person, type SuperTabState } from './model.js'import { projectsOf, type SwarmProject } from './projects.js'import { failureNotice, publicReason } from './startFailure.js'@@ -32,6 +32,8 @@ export interface LeadContext {teamMachines?: readonly string[]otherWorkspaceMachines?: readonly string[]teamAccountsIdle?: boolean+ /** The slots of each pool, counted once for a read that admits many (machines.ts slotCounter). */+ slots?: (pool: ContributedMachine) => number}/** The engines a Product Manager runs on, as people read them. */@@ -42,7 +44,7 @@ function admitLead(state: SuperTabState, project: SwarmProject, pool: Contributeif (!contributedEngines(pool).includes(start.engine)) {fail('ENGINE_NOT_OFFERED', `${LEAD_ENGINES[start.engine as keyof typeof LEAD_ENGINES] ?? start.engine} isn’t offered on ${pool.name}.`, 403)}- admitMachineStart(state, start, context.now, undefined, context.teamMachines, context.otherWorkspaceMachines, project)+ admitMachineStart(state, start, context.now, undefined, context.teamMachines, context.otherWorkspaceMachines, project, context.slots)// Its Team account will serve only this project (machines.ts productManagerOn), so not one running another's work now.if (teamAccountPool(pool) && [...projectsOnMachine(state, pool.machineId)].some(id => id !== project.id)) {fail('TEAM_ACCOUNT_BUSY', `The Harness Team account on ${pool.name} runs other projects' agents, so it cannot host this ${LEAD_NAME} now.`, 409)@@ -115,6 +117,9 @@ export function leadChoices(state: SuperTabState, personId: string, context: Leaconst project = { id: '', name: '', ownerId: personId, leadConversationId: '', visibility: 'private', members: [], updateMinutes: 5,skillMode: 'default' } as unknown as SwarmProjectconst leadFor = { projectId: '', personId, generation: by.generation }+ // Every teammate's machine and engine is admitted against this one state: its slots are counted once (prod 10-31: a quarter+ // of a change read's CPU, each admission grouping the Swarm's 543 released harnesses again).+ context = { ...context, slots: context.slots ?? slotCounter(state) }const choices: Record<string, Record<string, { machineId: string } | { refused: string }>> = {}for (const machine of state.machines ?? []) {if (machine.hostMachineId || machine.removal || machine.contributorId === personId) continuediff --git a/backend/src/superTabs/machineSlots.test.ts b/backend/src/superTabs/machineSlots.test.tsnew file mode 100644index 000000000..73bdfd702--- /dev/null+++ b/backend/src/superTabs/machineSlots.test.ts@@ -0,0 +1,72 @@+import { describe, expect, it } from 'vitest'+import { machineSlots, slotCounter, teamPoolOf, type ContributedMachine } from './machines.js'+import { startFinished } from './parts/write.js'+import type { SuperTabState } from './model.js'++/** The count before slotCounter: each pool filtered the Swarm's whole lists (prod 10-31: 543 released harnesses, 17 machines,+ * ~27 k callbacks a change read). Kept here only as the reference the counter must equal. */+function referenceSlots(state: SuperTabState, pool: ContributedMachine): number {+ const poolSlots = (each: ContributedMachine) => {+ const reservations = (state.harnessReservations ?? []).filter(r => r.machineId === each.machineId)+ const live = new Set(reservations.map(r => r.harnessId))+ const released = (state.releasedHarnesses ?? []).filter(r => r.machineId === each.machineId && !live.has(r.harnessId)).map(r => r.harnessId)+ const observed = each.observation?.stoppedHarnessIds ?? []+ const stopped = new Set([...observed.filter(id => {+ const reservation = reservations.find(r => r.harnessId === id)+ return !reservation || !reservation.renewed || each.observation?.stoppedReservations?.some(r => r.harnessId === id && r.generation === reservation.generation)+ }), ...released])+ const running = new Set((each.observation?.runningHarnessIds ?? []).filter(id => !stopped.has(id)))+ for (const a of state.agents) if (a.machineId === each.machineId && !stopped.has(a.harnessId)) running.add(a.harnessId)+ for (const r of reservations) if (!stopped.has(r.harnessId)) running.add(r.harnessId)+ let pending = 0+ for (const start of state.starts ?? []) {+ if (start.request.machineId !== each.machineId || start.error) continue+ if (start.harnessId) { if (!startFinished(start) && !stopped.has(start.harnessId)) running.add(start.harnessId) }+ else pending+++ }+ return running.size + pending+ }+ const mac = pool.hostMachineId ? state.machines?.find(m => m.machineId === pool.hostMachineId && m.contributorId === pool.contributorId) : pool+ const team = mac && teamPoolOf(state, mac)+ return [...new Set([pool, mac, team])].reduce((sum, each) => sum + (each ? poolSlots(each) : 0), 0)+}++/** A small seeded generator, so a failure names the state that broke. */+function random(seed: number) {+ let s = seed+ const next = () => { s = (s * 1103515245 + 12345) % 2 ** 31; return s / 2 ** 31 }+ return { next, pick: <T>(list: readonly T[]) => list[Math.floor(next() * list.length)]!, chance: (p: number) => next() < p }+}++function swarmOf(seed: number): SuperTabState {+ const r = random(seed)+ const machineIds = ['m1', 'm2', 'm3', 'mac']+ const harness = () => `h${Math.floor(r.next() * 12)}`+ const machines = machineIds.map(machineId => ({ machineId, contributorId: 'p1', maxAgents: 4,+ ...(machineId !== 'mac' && r.chance(0.3) ? { hostMachineId: 'mac' } : {}),+ observation: { runningHarnessIds: Array.from({ length: 3 }, harness), stoppedHarnessIds: Array.from({ length: 3 }, harness),+ ...(r.chance(0.5) ? { stoppedReservations: [{ harnessId: harness(), generation: r.pick(['g1', 'g2']) }] } : {}) } })) as unknown as ContributedMachine[]+ return {+ machines,+ harnessReservations: Array.from({ length: 6 }, () => ({ machineId: r.pick(machineIds), harnessId: harness(), generation: r.pick(['g1', 'g2']), renewed: r.chance(0.5) })),+ releasedHarnesses: Array.from({ length: 40 }, () => ({ machineId: r.pick(machineIds), harnessId: harness() })),+ agents: Array.from({ length: 8 }, (_, i) => ({ id: `a${i}`, machineId: r.pick(machineIds), harnessId: harness() })),+ starts: Array.from({ length: 10 }, (_, i) => ({ id: `s${i}`, request: { machineId: r.pick(machineIds) },+ ...(r.chance(0.2) ? { error: 'failed' } : {}), ...(r.chance(0.6) ? { harnessId: harness() } : {}),+ ...(r.chance(0.5) ? { poolGeneration: 'g1', ...(r.chance(0.5) ? { poolSettled: true } : {}) } : {}) })),+ } as unknown as SuperTabState+}++describe('slotCounter (machines.ts)', () => {+ it('counts every pool exactly as filtering the Swarm\'s lists per pool did, over many states', () => {+ for (let seed = 1; seed <= 400; seed++) {+ const state = swarmOf(seed), count = slotCounter(state)+ for (const pool of state.machines!) expect(count(pool), `seed ${seed} ${pool.machineId}`).toBe(referenceSlots(state, pool))+ }+ })++ it('answers as machineSlots does, which commands still call on the state they are changing', () => {+ const state = swarmOf(7)+ for (const pool of state.machines!) expect(machineSlots(state, pool)).toBe(slotCounter(state)(pool))+ })+})diff --git a/backend/src/superTabs/machines.ts b/backend/src/superTabs/machines.tsindex c4fc1e23a..a0572b225 100644--- a/backend/src/superTabs/machines.ts+++ b/backend/src/superTabs/machines.ts@@ -563,33 +563,61 @@ export function removalEnded(state: SuperTabState, pool: ContributedMachine, how/** Pending intents reserve slots too. Only an explicit host stop frees a launched slot. A Mac and its Harness Team account* share the Mac's slots (One machine, teamStartRequest): each counts what runs on both. */export function machineSlots(state: SuperTabState, pool: ContributedMachine) {- const mac = pool.hostMachineId ? state.machines?.find(m => m.machineId === pool.hostMachineId && m.contributorId === pool.contributorId) : pool- const team = mac && teamPoolOf(state, mac)- return [...new Set([pool, mac, team])].reduce((sum, each) => sum + (each ? poolSlots(state, each) : 0), 0)-}--function poolSlots(state: SuperTabState, pool: ContributedMachine) {- const reservations = (state.harnessReservations ?? []).filter(r => r.machineId === pool.machineId)- const live = new Set(reservations.map(r => r.harnessId))- const released = (state.releasedHarnesses ?? []).filter(r => r.machineId === pool.machineId && !live.has(r.harnessId)).map(r => r.harnessId)- const observed = pool.observation?.stoppedHarnessIds ?? []- const stopped = new Set([...observed.filter(id => {- const reservation = reservations.find(r => r.harnessId === id)- // Older hosts can prove a first launch stopped, but cannot distinguish a resumed runtime from its predecessor.- return !reservation || !reservation.renewed || pool.observation?.stoppedReservations?.some(r => r.harnessId === id && r.generation === reservation.generation)- }), ...released])- const running = new Set((pool.observation?.runningHarnessIds ?? []).filter(id => !stopped.has(id)))- for (const a of state.agents) if (a.machineId === pool.machineId && !stopped.has(a.harnessId)) running.add(a.harnessId)- for (const r of reservations) if (!stopped.has(r.harnessId)) running.add(r.harnessId)- let pending = 0- for (const start of state.starts ?? []) {- if (start.request.machineId !== pool.machineId || start.error) continue- // A finished start's harness counts through its agent or its host's report. Reads keep finished starts until they expire,- // and counted every harness ever started there (Mac Studio, 10-29: 13 running, 121 counted, no free agents shown).- if (start.harnessId) { if (!startFinished(start) && !stopped.has(start.harnessId)) running.add(start.harnessId) }- else pending+++ return slotCounter(state)(pool)+}++/**+ * The slots each pool of `state` holds, for every pool a read lists: the Swarm's reservations, released harnesses, agents+ * and starts are grouped by machine once, where each pool used to filter the whole lists again (prod 10-31: 543 released+ * harnesses on 17 machines, ~27 k callbacks per change read, a quarter of its CPU). Counts the state as it is when called;+ * a command that changes the state asks again (machineSlots).+ */+export function slotCounter(state: SuperTabState): (pool: ContributedMachine) => number {+ const group = <T>(items: readonly T[] | undefined, machineOf: (item: T) => string) => {+ const by = new Map<string, T[]>()+ for (const item of items ?? []) {+ const list = by.get(machineOf(item))+ if (list) list.push(item)+ else by.set(machineOf(item), [item])+ }+ return by+ }+ const reservationsBy = group(state.harnessReservations, r => r.machineId)+ const releasedBy = group(state.releasedHarnesses, r => r.machineId)+ const agentsBy = group(state.agents, a => a.machineId)+ const startsBy = group(state.starts, s => s.request.machineId)+ const counted = new Map<ContributedMachine, number>()+ const poolSlots = (pool: ContributedMachine) => {+ const known = counted.get(pool)+ if (known !== undefined) return known+ const reservations = reservationsBy.get(pool.machineId) ?? []+ const live = new Set(reservations.map(r => r.harnessId))+ const released = (releasedBy.get(pool.machineId) ?? []).filter(r => !live.has(r.harnessId)).map(r => r.harnessId)+ const observed = pool.observation?.stoppedHarnessIds ?? []+ const stopped = new Set([...observed.filter(id => {+ const reservation = reservations.find(r => r.harnessId === id)+ // Older hosts can prove a first launch stopped, but cannot distinguish a resumed runtime from its predecessor.+ return !reservation || !reservation.renewed || pool.observation?.stoppedReservations?.some(r => r.harnessId === id && r.generation === reservation.generation)+ }), ...released])+ const running = new Set((pool.observation?.runningHarnessIds ?? []).filter(id => !stopped.has(id)))+ for (const a of agentsBy.get(pool.machineId) ?? []) if (!stopped.has(a.harnessId)) running.add(a.harnessId)+ for (const r of reservations) if (!stopped.has(r.harnessId)) running.add(r.harnessId)+ let pending = 0+ for (const start of startsBy.get(pool.machineId) ?? []) {+ if (start.error) continue+ // A finished start's harness counts through its agent or its host's report. Reads keep finished starts until they expire,+ // and counted every harness ever started there (Mac Studio, 10-29: 13 running, 121 counted, no free agents shown).+ if (start.harnessId) { if (!startFinished(start) && !stopped.has(start.harnessId)) running.add(start.harnessId) }+ else pending+++ }+ counted.set(pool, running.size + pending)+ return running.size + pending+ }+ return pool => {+ const mac = pool.hostMachineId ? state.machines?.find(m => m.machineId === pool.hostMachineId && m.contributorId === pool.contributorId) : pool+ const team = mac && teamPoolOf(state, mac)+ return [...new Set([pool, mac, team])].reduce((sum, each) => sum + (each ? poolSlots(each) : 0), 0)}- return running.size + pending}/** Machines of the Swarm serving models on its grid now, by model: only fresh readings count. */@@ -629,7 +657,9 @@ export function localModelAllowed(state: SuperTabState, request: AgentStartReque/** [forProject]: the project it is for when that is not yet its conversation's, as New Project's Product Manager before the* project exists (leadStart.ts leadChoices). */export function admitMachineStart(state: SuperTabState, request: AgentStartRequest, now: number, start?: AgentStart, teamMachines: readonly string[] = [],- otherWorkspaceMachines: readonly string[] = [], forProject?: SwarmProject) {+ otherWorkspaceMachines: readonly string[] = [], forProject?: SwarmProject,+ /** A read that admits many machines against one state counts their slots once (leadStart.ts leadChoices). */+ slots: (pool: ContributedMachine) => number = pool => machineSlots(state, pool)) {const pool = machineContribution(state, request.machineId)if (!pool) return nullif (pool.buddyOnly && request.buddyOf?.personId !== pool.contributorId) fail('NOT_SWARM_MACHINE', NOT_SWARM_MACHINE, 403)@@ -668,7 +698,7 @@ export function admitMachineStart(state: SuperTabState, request: AgentStartReque}// A Buddy-only machine is recorded with its Buddy's start, before its host ever read the Swarm: the start waits for it.if (machineOffline(pool, now) && !(pool.buddyOnly && !pool.observation)) fail('MACHINE_OFFLINE', 'Wait for a fresh reading from the contributed machine.')- if (!start && machineSlots(state, pool) >= pool.maxAgents) fail('MACHINE_FULL', 'This machine has reached its contributed agent limit.')+ if (!start && slots(pool) >= pool.maxAgents) fail('MACHINE_FULL', 'This machine has reached its contributed agent limit.')// A new start only: a grid model runs without the engine's subscription, and one already asked for is settled by its host.const unusable = !start && !local ? engineUnusable(pool, request.engine, now) : nullif (unusable) fail('ENGINE_UNAVAILABLE', unusable)diff --git a/backend/src/superTabs/read/views.ts b/backend/src/superTabs/read/views.tsindex f4d40a73c..8391ec609 100644--- a/backend/src/superTabs/read/views.ts+++ b/backend/src/superTabs/read/views.ts@@ -7,7 +7,7 @@ import { suspendedEngines } from '../engineHealth.js'import { leadChoices } from '../leadStart.js'import { isBuddyChat, isBuddyReader } from '../buddies.js'import { agentForBuddy, agentWithoutMachine, groupWithoutMachines, isLeads, machineHiddenFrom, seenMachineIds, visibleState, withSeenRequirement, type Actor, type Agent, type Conversation, type SuperTabState } from '../model.js'-import { machineSlots, machineTeamWork, teamAccountSwarm, type ContributedMachine } from '../machines.js'+import { slotCounter, machineTeamWork, teamAccountSwarm, type ContributedMachine } from '../machines.js'import * as store from '../parts/index.js'import type { Part, SwarmParts } from '../parts/index.js'import { canReadGroup } from '../projects.js'@@ -123,12 +123,14 @@ function derive(state: SuperTabState, visible: SuperTabState, actor: Actor, now:? leadChoices(state, actor.userId, { now, teamMachines: extras.teamMachines, otherWorkspaceMachines: extras.otherWorkspaceMachines ?? [],...(extras.teamAccountsIdle ? { teamAccountsIdle: true } : {}) }): {}+ // Every listed machine's slots from one pass over the Swarm's lists (machines.ts slotCounter).+ const slots = slotCounter(state)const machine = (pool: ContributedMachine) => {const stored = state.machines!.find(m => m.machineId === pool.machineId)!const refusal = machineTeamWork(state, stored, now, extras.teamMachines, extras.otherWorkspaceMachines ?? [])const version = extras.machineVersions?.[pool.machineId]const suspended = suspendedEngines(stored, now)- return [pool.machineId, { availableSlots: Math.max(0, pool.maxAgents - machineSlots(state, stored)),+ return [pool.machineId, { availableSlots: Math.max(0, pool.maxAgents - slots(stored)),teamWork: refusal ? { allowed: false as const, ...refusal } : { allowed: true as const }, ...(version ? { clientVersion: version } : {}),...(Object.keys(suspended).length ? { suspendedEngines: suspended } : {}), ...(leads[pool.machineId] ? { lead: leads[pool.machineId] } : {}) }] as const}diff --git a/backend/src/superTabs/store.ts b/backend/src/superTabs/store.tsindex 936bc878f..aa857f8d3 100644--- a/backend/src/superTabs/store.ts+++ b/backend/src/superTabs/store.ts@@ -1,4 +1,4 @@-import { expireRemovals, LASTING_TEAM_REFUSALS, machineContribution, machineObservationSchema, machineSlots, machineTeamWork, preparableStart, readingOnly, recordAgentPresence, removalPreview, startPreparing, storedObservation, swarmGridRow, teamAccountRequired, teamWorkRefusal, type MachineObservation } from './machines.js'+import { expireRemovals, LASTING_TEAM_REFUSALS, machineContribution, machineObservationSchema, slotCounter, machineTeamWork, preparableStart, readingOnly, recordAgentPresence, removalPreview, startPreparing, storedObservation, swarmGridRow, teamAccountRequired, teamWorkRefusal, type MachineObservation } from './machines.js'import { readingClearsSuspensions, suspendedEngines } from './engineHealth.js'import { BUDDY_OWN_MACHINE_REFUSAL } from './buddyContract.js'import { createHash } from 'node:crypto'@@ -1043,13 +1043,14 @@ export class SuperTabStore {const facts = visible.machines?.length ? await this.machineFacts(tx, state) : { team: [], versions: {} as Record<string, string> }const teamMachines = facts.teamconst otherWorkspace = visible.machines?.length ? await this.otherWorkspaceMachines(tx, state) : []+ const slots = slotCounter(state)return { lastChangedAt: await readLastChangedAt(tx, view, tabId) ?? 0, state: { ...visible, ...mentions, machines: visible.machines?.map(pool => {const stored = state.machines!.find(m => m.machineId === pool.machineId)!// Whether it takes teammates' projects now, and if not why (machines.ts teamWorkRefusal): what routing leaves it// out for, and what Desktop can show on the machine.const refusal = machineTeamWork(state, stored, now, teamMachines, otherWorkspace)const suspended = suspendedEngines(stored, now)- return { ...pool, availableSlots: Math.max(0, pool.maxAgents - machineSlots(state, stored)),+ return { ...pool, availableSlots: Math.max(0, pool.maxAgents - slots(stored)),teamWork: refusal ? { allowed: false as const, ...refusal } : { allowed: true as const },...(facts.versions[pool.machineId] ? { clientVersion: facts.versions[pool.machineId] } : {}),// What the route leaves out (cli superTabs/routeCommand.ts unreadyPool).