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.

3.4 KB
Raw Blame
/**
 * A project's crew (task #3397, anh Dee's Swarm redesign, 11-05): named agents the Swarm keeps alive. Their host reopens a
 * crashed or frozen session in place (cli superTabs/crew.ts), and no idle path closes or removes one: the CLI's idle cleanup
 * (its retention names crew), the coordinator's idle notice (tidy.ts) and the removal of closed agents (closedAgents.ts).
 * A crashed crew member keeps its task for CREW_REOPEN_GRACE_MS, while its host reopens it (liveness.ts).
 *
 * The numbers here stay in this one place until the project rules record lands (sibling task of #3397).
 */
import { DISPATCH_RULES, laneNamed, spokenList, type Lane } from './dispatch.js'
import { isBuddy } from './buddies.js'
import { roleNameOf } from './instructions.js'
import { fail, type Actor, type Agent, type SuperTabState } from './model.js'
import { agentProjectId, canReadProject, ledProject, ownsProject, projectOf } from './projects.js'

/** How long a crew member whose session stopped keeps its task while its host reopens it in place. */
export const CREW_REOPEN_GRACE_MS = 5 * 60_000
/** The longest role a crew entry names, in characters. */
export const CREW_ROLE_MAX = 40
export const isCrew = (target: Pick<Agent, 'crew'>): boolean => !!target.crew

/** Who may change [target]'s crew mark: its project's PM, or, of the people who read the project and are no viewer, the
 *  Swarm's owner, the project's owner, the person who runs the agent or the owner of the machine it runs on, as agent.release
 *  takes (River for security review 1 of #3222, before the Swarm goes public): a crew mark makes that machine reopen it. */
function mayManageCrew(state: SuperTabState, actor: Actor, target: Agent): void {
  const project = projectOf(state, agentProjectId(state, target))
  const allowed = actor.kind === 'agent'
    ? ledProject(state, actor.agentId)?.id === project.id
    : state.people.find(p => p.id === actor.userId)?.role !== 'viewer' && canReadProject(state, actor, project)
      && (actor.userId === state.ownerId || ownsProject(state, actor.userId, project) || [target.contributorId, target.machineOwnerId].includes(actor.userId))
  if (!allowed) fail('FORBIDDEN', 'Only the project\'s PM or the person who runs this agent, its machine\'s owner or the project\'s owner can change its crew.', 403)
}

/** `crew.add`: marks [target] as crew, with [role] or the role it goes by, and its [lane] if any. Again on crew replaces it.
 *  Never the PM (kept alive already) nor a Buddy. */
export function addCrew(state: SuperTabState, actor: Actor, target: Agent, role: string | undefined, lane: string | undefined, now: number, lanes: readonly Lane[] = DISPATCH_RULES.lanes): void {
  mayManageCrew(state, actor, target)
  if (isBuddy(target)) fail('CREW_BUDDY', 'A Buddy waits for its person; it is never crew.')
  if (ledProject(state, target.id)) fail('CREW_LEAD', 'The PM is already kept alive; it needs no crew mark.')
  if (lane) lane = (laneNamed(lanes, lane) ?? fail('LANE_UNKNOWN', `Name a lane: ${spokenList(lanes.map(l => l.name))}.`, 400)).toLowerCase()
  target.crew = { role: role ?? roleNameOf(state, target).slice(0, CREW_ROLE_MAX), ...lane ? { lane } : {}, at: now, by: actor.kind === 'agent' ? actor.agentId : actor.userId }
}

/** `crew.remove`: [target] goes back to the ordinary idle handling. */
export function removeCrew(state: SuperTabState, actor: Actor, target: Agent): void {
  mayManageCrew(state, actor, target)
  delete target.crew
}