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
import { appPullRequestMerges } from '../superTabs/ruleMoves.js'
import { declaredHttpClient } from '../superTabs/refusalLog.js'
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
import { z, ZodError } from 'zod'
import { prisma, prismaSecondary } from '../lib/prisma.js'
import { env } from '../config/env.js'
import { phoneSendersFromEnv, sendPhoneNotices } from '../push/send.js'
import multipart from '@fastify/multipart'
import { attachmentsFromEnv, formFileBody, FORM_TOO_LARGE_MESSAGE, MAX_FORM_ATTACHMENT_BYTES, MAX_AVATAR_BYTES, stopUnpacking, unpackFailed } from '../superTabs/attachments.js'
import { validateBody, validateParams, validateQuery } from '../middlewares/validation.js'
import { sendError, sendSuccess } from '../utils/response.js'
import { operationSchema } from '../superTabs/commands.js'
import { PERIODS } from '../superTabs/leaderboard/rolling.js'
import { fail, SuperTabError } from '../superTabs/model.js'
import { SuperTabStore } from '../superTabs/store.js'
import { redisPollMemory } from '../superTabs/pollMemory.js'
import { redisAutoJoinMemory } from '../superTabs/autoJoin.js'
import { redisReportMemory } from '../superTabs/machineReports.js'
import { routingPolicySaveSchema } from '../superTabs/routingPolicy.js'
import { nativeEventSchema, openNativeSchema } from '../superTabs/native.js'
import { machineObservationSchema, teamAccountRequired } from '../superTabs/machines.js'
import { repositoryReachOf } from '../superTabs/repositoryReach.js'
import { joinSwarmSchema, previewJoinSchema, saveSharingSchema } from '../superTabs/sharing.js'
import { holdReason } from '../superTabs/holdReason.js'
import { GitHubAppError } from '../github/app.js'
import { projectGitHubAccess } from '../github/projectAccess.js'
import { projectCiLoad } from '../github/ciLoad.js'
import { projectBranchOrigin, projectPushOrigin } from '../github/pushOrigin.js'
import { projectPullRequestFiles } from '../superTabs/pullRequestFiles.js'
import { appPullRequestWatch } from '../superTabs/pullRequestEvents.js'
import { githubSignIn, PrismaApprovals, readState, signState, stateSecret, type Approvals, type GitHubSignIn } from '../github/approvals.js'
import type { GitHubRepository } from '../github/repository.js'
import { githubRepository } from '../github/repository.js'
import { attribution } from '../github/attribution.js'
import { createRepository, createStateSecret, GITHUB_OWNER, ownerCheckAllowed, readCreateState, repositoryNameProblem, signCreateState } from '../github/repositoryCreate.js'
import { githubTokensFromEnv, type GitHubTokens } from '../github/tokens.js'
import { logger } from '../utils/logger.js'
import { SwarmReads } from '../superTabs/read/service.js'
import { inboxWait, readInbox } from '../superTabs/read/inbox.js'
import { READ_THREADS_ASKED_MAX, readFollowedThreads, threadsWait } from '../superTabs/read/followedThreads.js'
import { activityWait } from '../superTabs/activity.js'
import { reminderWait } from '../superTabs/reminders.js'
import { NOTIFY_LEVELS } from '../superTabs/chatSettings.js'
import { HostReads } from '../superTabs/read/host.js'
import { UPDATE_REQUIRED } from '../superTabs/read/contract.js'
import { isTeamSession, TEAM_SESSION_LIMITED, TEAM_SESSION_MESSAGE } from '../lib/teamSession.js'
import type { AuthUser } from '../lib/ssoAuth.js'
import { DEFAULT_DAYS, MAX_DAYS } from '../superTabs/costs/read.js'
import { WORKSPACE_ACCESS_REVOKED } from '../superTabs/workspaceStore.js'
import { markNothingNew } from '../lib/routeLatency.js'
import { onMachineRenamed } from '../superTabs/machineRenames.js'
import { onComputerSignedIn } from '../superTabs/computerMoves.js'
/** Runs a poll and marks its reply nothing-new for `route latency` when `empty` says so (spec 2026-11-02 poll cost, Part 0). */
const noting = <T>(req: FastifyRequest, run: () => Promise<T>, empty: (result: T) => boolean) => async (): Promise<T> => {
const result = await run()
if (empty(result)) markNothingNew(req)
return result
}
/** A change read that found nothing: no reload, no head, and every list of changed parts empty. */
const nothingChanged = (changes: object): boolean =>
(changes as { reload?: unknown }).reload === false && (changes as { head?: unknown }).head == null
&& Object.values(changes).every(value => !Array.isArray(value) || value.length === 0)
const id = z.string().min(1).max(160).regex(/^[A-Za-z0-9_-]+$/)
const tabParams = z.object({ tabId: id })
const agentQuery = z.object({ asAgentId: id.optional() }).strict()
const uploadParams = z.object({ tabId: id, uploadId: id })
const voiceParams = z.object({ tabId: id, attachmentId: id })
const voiceQuery = agentQuery.extend({ lang: z.string().max(16).optional() })
// The store checks the name, type and size; this only bounds what reaches it.
const uploadLinkBody = z.object({ conversationId: id, name: z.string().max(1000), mime: z.string().max(1000), size: z.number().int() }).strict()
const emptyBody = z.object({}).strict().optional()
const create = z.object({ operationId: id, name: z.string().trim().min(1).max(160), projectVisibility: z.enum(['public', 'private']).optional(),
workspaceId: id.optional() }).strict()
const operation = operationSchema.extend({ asAgentId: id.optional() })
// Workspaces (superTabs/workspaceStore.ts). A Team session (lib/teamSession.ts) never acts on a Workspace: only the signed-in person does.
const workspaceParams = z.object({ workspaceId: id })
const workspaceMemberParams = z.object({ workspaceId: id, userId: id })
const workspaceName = z.string().trim().min(1).max(160)
const workspaceCreate = z.object({ operationId: id, name: workspaceName }).strict()
const workspaceRename = z.object({ name: workspaceName }).strict()
const workspaceInvite = z.object({ email: z.email().max(320), role: z.enum(['admin', 'member', 'guest']) }).strict()
const workspaceRevoke = z.object({ email: z.email().max(320) }).strict()
const workspaceRole = z.object({ role: z.enum(['admin', 'member', 'guest']) }).strict()
const workspaceTransfer = z.object({ userId: id }).strict()
const workspaceVisibility = z.object({ visibility: z.enum(['invite-only', 'workspace']) }).strict()
const workspaceJoin = z.object({ operationId: id }).strict()
const personOnly = (user: AuthUser) => { if (isTeamSession(user)) fail(TEAM_SESSION_LIMITED, TEAM_SESSION_MESSAGE, 403) }
const listQuery = z.object({ after: id.optional(), mentions: z.literal('1').optional() }).strict()
const cursor = z.string().min(1).max(512).regex(/^[A-Za-z0-9_-]+$/)
// `derivedEtag`: the summary's etag the client holds (read/derivedEtag.ts, task #2906).
const changesQuery = agentQuery.extend({ cursor, machines: z.literal('light').optional(),
derivedEtag: z.string().min(1).max(64).regex(/^[A-Za-z0-9_-]+$/).optional() })
const costsQuery = z.object({ days: z.coerce.number().int().min(1).max(MAX_DAYS).default(DEFAULT_DAYS), asAgentId: id.optional() }).strict()
const scorecardQuery = z.object({ projectId: id, scope: z.literal('all').optional() }).strict()
const leaderboardQuery = z.object({ projectId: id, week: z.coerce.number().int().nonnegative().safe().optional(), period: z.enum(PERIODS).optional(), scope: z.literal('all').optional() }).strict()
.refine(query => query.week === undefined || query.period === undefined, 'Ask for a week or a period, not both.')
const dormantQuery = agentQuery.extend({ dormant: z.literal('1'), projectId: id.optional(), before: cursor.optional() })
/** An older page of the person's Tasks feed (task #4203): the `olderCursor` a first load or an earlier page gave. */
const personalTasksQuery = z.object({ before: cursor.optional() }).strict()
const groupParams = tabParams.extend({ conversationId: id })
const hostChangesQuery = z.object({ cursor: cursor.optional(), full: z.literal('1').optional() }).strict()
const etag = z.string().regex(/^[A-Za-z0-9_-]{1,64}$/)
const hostChangesBody = z.object({ cursor: cursor.optional(), full: z.boolean().optional(),
held: z.record(id, etag).refine(held => Object.keys(held).length <= 500, 'At most 500 Swarms.').optional() }).strict()
const startParams = tabParams.extend({ startId: id })
const nativeParams = tabParams.extend({ contextId: id })
const messageParams = tabParams.extend({ conversationId: id })
// A person's own read mark (superTabs/readMarks.ts): no asAgentId, since agents keep none.
const readMarkBody = z.object({ sequence: z.number().int().nonnegative().safe() }).strict()
// Mark unread names the message it starts from: the mark goes to just before it.
const unreadBody = z.object({ sequence: z.number().int().positive().safe() }).strict()
const followBody = z.object({ follow: z.boolean().nullable() }).strict()
const notifyBody = z.object({ notify: z.enum(NOTIFY_LEVELS) }).strict()
const activityReadBody = z.object({ through: z.string().datetime() }).strict()
// A time sets or replaces the reminder; null clears it. One POST, as Desktop reaches the backend through the daemon, which
// forwards only GET and POST (cli superTabs/service.ts), and the web's transport sends no other.
const reminderBody = z.object({ at: z.string().datetime({ offset: true }).nullable() }).strict()
const threadParams = messageParams.extend({ threadId: id })
const fileParams = messageParams.extend({ messageId: id, attachmentId: id })
const reminderParams = messageParams.extend({ messageId: id })
const noQuery = z.object({}).strict()
/** The followed-threads read's query (read/followedThreads.ts): how many read threads to answer. */
const threadsQuery = z.object({ read: z.string().regex(/^[1-9][0-9]{0,2}$/).transform(Number)
.refine(n => n <= READ_THREADS_ASKED_MAX, `At most ${READ_THREADS_ASKED_MAX}.`).optional() }).strict()
// The project whose open Buddy proposals a person reads (superTabs/proposals.ts).
const proposalsQuery = z.object({ projectId: id }).strict()
/** The Cmd-N and join project list's query (superTabs/projectList.ts): the project the person was invited to, if any. */
const projectListQuery = z.object({ invitedTo: id.optional() }).strict()
// The reader's own catch-up (superTabs/catchUp.ts): no asAgentId, since agents read every message as it comes.
const catchUpQuery = z.object({ after: z.coerce.number().int().nonnegative().safe() }).strict()
// Pages by (sequence, id) (design 4.6): `afterId`/`beforeId` go with `after`/`before`, from the previous page's `next`.
const messageQuery = agentQuery.extend({ after: z.coerce.number().int().nonnegative().safe().optional(),
before: z.coerce.number().int().nonnegative().safe().optional(), messageId: id.optional(), afterId: id.optional(), beforeId: id.optional(),
// One thread's root and replies, paged like the conversation.
thread: id.optional() })
.refine(q => [q.after, q.before, q.messageId].filter(value => value !== undefined).length <= 1, 'Choose one message cursor or exact message.')
.refine(q => (q.afterId === undefined || q.after !== undefined) && (q.beforeId === undefined || q.before !== undefined), 'Send afterId with after, beforeId with before.')
const decisionParams = tabParams.extend({ decisionId: id })
const roleParams = tabParams.extend({ projectId: id, roleId: id })
const revisionParams = tabParams.extend({ projectId: id, revision: z.coerce.number().int().positive().safe() })
const terminalParams = tabParams.extend({ requestId: id })
const hostParams = z.object({ machineId: id })
const hostStartParams = hostParams.extend({ tabId: id, startId: id })
const machineParams = hostParams.extend({ tabId: id })
const projectParams = tabParams.extend({ projectId: id })
// `record=0`: an app reading again because the project's GitHub revision moved, not a person checking; it records nothing.
const refreshQuery = z.object({ refresh: z.literal('1').optional(), record: z.literal('0').optional() })
const approveBody = z.object({ scope: z.enum(['project', 'swarm']) }).strict()
// New Project's "New repository" (task #1788): an organization the person picked, and the repository's name and privacy.
const ownerParams = tabParams.extend({ owner: z.string().regex(GITHUB_OWNER) })
const repositoryBody = z.object({ owner: z.string().regex(GITHUB_OWNER), name: z.string().max(200), private: z.boolean() }).strict()
// What GitHub sends back: a sign-in's code and our state, and after an installation its id and action.
const callbackQuery = z.object({ code: z.string().max(512).optional(), state: z.string().max(4096).optional(), error: z.string().max(128).optional(),
installation_id: z.string().max(32).optional(), setup_action: z.string().max(32).optional() })
const escapeHtml = (text: string) => text.replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]!)
/** The page a person sees in their browser after GitHub sends them back. */
function githubPage(title: string, text: string): string {
return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">`
+ `<title>${escapeHtml(title)}</title><style>body{font:16px/1.5 -apple-system,system-ui,sans-serif;max-width:32rem;margin:15vh auto;padding:0 1rem;`
+ `color:#1f2328;background:#fff}@media(prefers-color-scheme:dark){body{color:#e6edf3;background:#0d1117}}h1{font-size:1.4rem}</style></head>`
+ `<body><h1>${escapeHtml(title)}</h1><p>${escapeHtml(text)}</p></body></html>`
}
const deliveryParams = tabParams.extend({ agentId: id })
/** A PM's host says whether its turn is open with its delivery list read (store.ts deliveries, #3545); an older host sends nothing. */
const deliveryListQuery = z.object({ turn: z.enum(['idle', 'busy']).optional() }).strict()
const deliveryActionParams = deliveryParams.extend({ deliveryId: id })
const deliveryAction = z.discriminatedUnion('type', [
z.object({ type: z.literal('claim'), claimId: id }).strict(),
z.object({ type: z.literal('ack'), claimId: id, receipt: z.string().min(1).max(2000), uncertain: z.boolean() }).strict(),
z.object({ type: z.literal('hold'), claimId: id, reason: holdReason }).strict(),
])
const mentionQuery = z.object({ state: z.enum(['waiting', 'earlier']) }).strict()
const personDecisionQuery = agentQuery.extend({ state: z.enum(['open', 'answered', 'settled']) }).strict()
const savePolicy = z.object({ revision: z.number().int().nonnegative().safe(), policy: routingPolicySaveSchema }).strict()
const stop = z.object({ assignmentId: id, receipt: z.string().min(1).max(2000) }).strict()
const githubToken = z.object({ machineId: id }).strict()
const githubStartToken = z.object({ always: z.boolean().optional() }).strict()
/**
* The routes about one Swarm that a sign-in may call: the web with the browser's own sign-in (desktop
* super_tabs/backend_transport.dart), Desktop and the phone through their machine, and a machine's host and its agents.
* On these, a Swarm, chat, message, project, decision, start, delivery, file or link the caller cannot reach gets one
* answer, whether it is missing, refused, revoked, outside its Workspace or in another project, so the answer never says
* which. The server log keeps the specific reason. Role refusals inside a Swarm the caller reads (read-only,
* owner-only) keep their own words: they tell nothing the caller cannot already see. Hosts read 403 and 404 alike as
* access lost (cli superTabs/machines.ts, swarmReads.ts).
*/
const NOT_AVAILABLE_EXEMPT_ROUTES: ReadonlyMap<string, string> = new Map([
// This retired endpoint never reads a Swarm: all callers must upgrade from full-state reads.
['GET /api/super-tabs/:tabId', 'Always returns UPDATE_REQUIRED without looking up the Swarm.'],
])
/** The one answer on those routes for anything the caller cannot reach. 404 with the code every client already
* reads as gone (Desktop and the phone: FORBIDDEN or NOT_FOUND; the CLI: 403 or 404). */
export const NOT_AVAILABLE = { code: 'NOT_FOUND', message: 'This is not available to you. It may have been removed, or your access changed.', status: 404 } as const
const UNREACHABLE_CODES: ReadonlySet<string> = new Set(['NOT_FOUND', 'JOIN_UNAVAILABLE', WORKSPACE_ACCESS_REVOKED])
const unreachable = (error: SuperTabError) => error.unreachable || UNREACHABLE_CODES.has(error.code)
/** At most this many Swarms' readings in one call; the most a machine was seen in is 21 (DB roadmap 2.5). */
export const READINGS_MAX = 64
/** A reading is a few KB, at most ~700 KB at its schema's limits (see HOT_BODY_LIMIT), so a batch gets a limit of its own. */
export const READINGS_BODY_LIMIT = 8 * 1024 * 1024
/** Each observation is checked on its own (machineObservationSchema, in the store), so one a backend refuses fails only its
* Swarm, and the host sends that one again through the per-Swarm route with its older fields (cli superTabs/machines.ts). */
const readingsBody = z.object({ readings: z.array(z.object({ tabId: id, observation: z.unknown() }).strict()).min(1).max(READINGS_MAX)
.refine(list => new Set(list.map(r => r.tabId)).size === list.length, 'Name each Swarm once.') }).strict()
/** One reading's refusal in a batch, answered as the per-Swarm route answers it: a Swarm the caller cannot reach gets the one
* NOT_AVAILABLE answer (the default for per-Swarm routes), anything unexpected a 500 for that Swarm alone, logged. */
export function readingError(tabId: string, error: unknown): { code: string; message: string; status: number } {
if (error instanceof ZodError) return { code: 'VALIDATION_ERROR', message: error.issues[0]?.message ?? 'This reading is not valid.', status: 400 }
if (error instanceof SuperTabError) {
if (!unreachable(error)) return { code: error.code, message: error.message, status: error.status }
logger.warn('Swarm not available to the caller', { code: error.code, statusCode: error.status, reason: error.message, tabId, path: 'readings' })
return { ...NOT_AVAILABLE }
}
logger.error('[super-tab] a machine reading failed', error instanceof Error ? error : new Error(String(error)), { tabId })
return { code: 'INTERNAL_ERROR', message: 'This reading could not be stored. It is sent again with the next one.', status: 500 }
}
async function respond(reply: FastifyReply, action: () => Promise<unknown>) {
try { sendSuccess(reply, await action()) } catch (error) {
if (error instanceof GitHubAppError) return sendError(reply, error.message, error.code, error.status, error.details)
const { request } = reply
if (error instanceof SuperTabError && unreachable(error)
&& /^\/api\/super-tabs\/:tabId(?:\/|$)/.test(request.routeOptions.url ?? '')
&& !NOT_AVAILABLE_EXEMPT_ROUTES.has(`${request.method} ${request.routeOptions.url}`)) {
logger.warn('Swarm not available to the caller', { code: error.code, statusCode: error.status, reason: error.message,
path: request.url, method: request.method })
return sendError(reply, NOT_AVAILABLE.message, NOT_AVAILABLE.code, NOT_AVAILABLE.status)
}
// A Swarm refusal, a busy Swarm (503 with Retry-After), an expired transaction or a duplicate key: the shared
// error handler answers each deliberately, and every other failure stays a 500 with its stack logged.
throw error
}
}
/** A terminal recording's host stops recording for good on a 409 (cli superTabs/native.ts). A duplicate key that outlived
* every transaction retry is not the recording's fault, so it answers busy, which the host retries. */
function busyOnDuplicate(error: unknown): never {
if ((error as { code?: unknown } | null)?.code === 'P2002') fail('SWARM_BUSY', 'This Swarm is busy. Try again in a moment.', 503)
throw error
}
/** Uses the normal authenticated control API. It never accepts caller-provided host/contributor identities.
* `github` is the Harness GitHub App (github/tokens.ts): read from the environment unless given, null when off. */
/** Full-state reads of clients from before Swarms were stored by parts (design 4.11): every old client reads one of them
* before or right after any action, so it meets this at once. */
/**
* The body limit of the routes hosts and agents call every second or every turn (roadmap 2.8), in place of Fastify's 1 MiB:
* a body is parsed whole on the event loop before its schema is checked. Each route's largest valid body, from its schema's
* limits with free text in three-byte characters (routes/superTabs.test.ts): host changes with 500 held Swarms 116 KB, a
* recorded turn 48 KB, a delivery ack or stop receipt 6 KB. Commands and machine readings keep the default: a task at its
* schema's limits reaches 700 KB.
*/
export const HOT_BODY_LIMIT = 128 * 1024
const updateRequired = (_req: unknown, reply: FastifyReply) => sendError(reply, UPDATE_REQUIRED.message, UPDATE_REQUIRED.code, UPDATE_REQUIRED.status)
/** The store this server runs on. Loaded only here: importing the bus opens its Redis connections. */
async function productionStore(github: GitHubTokens | null, approvals: Approvals): Promise<SuperTabStore> {
const [bus, { announceSwarmDeliveries, announceSwarmTerminal }, { announceSwarmHostChanges }, { announceSwarmChanges }, { tellSessionViewers },
{ VoiceQuotaExceededError, voiceQuotaService }, { pickWavLang, transcribeWavWithDeepgram }] = await Promise.all([
import('../lib/bus.js'), import('../lib/swarmDeliveryPushes.js'), import('../lib/swarmHostPushes.js'), import('../lib/swarmClientPushes.js'),
import('./harnessLinks.js'), import('../lib/voiceQuota.js'), import('../lib/deepgramWav.js')])
const phones = phoneSendersFromEnv(env)
return new SuperTabStore(prisma, Date.now, attachmentsFromEnv(env),
// Older history pages and the costs report read the secondaries (lib/prisma.ts, task #2970).
{ replica: prismaSecondary,
// Free crew members get their next task from the 30-second sweep and right after the writes that free one (task #3400).
// Here, not in the worker: its work.assign reads the GitHub facts this store's hooks below give.
dispatch: true,
// Thread replies and tags reach the phones of whoever they tell, while the app is closed (push/send.ts, task #2905).
phoneNotices: notices => { void sendPhoneNotices(prisma, phones, notices) },
// A task that ends revokes the token it was given; a revoke that fails is logged inside and never blocks the write.
workEnded: (tabId, ended) => { void github?.release(tabId, ended).catch(error => logger.warn('[github] release failed', { tabId, error: String(error) })) },
workStarted: (tabId, started) => { void github?.resume(tabId, started).catch(error => logger.warn('[github] resume failed', { tabId, error: String(error) })) },
// A team account has no git login of its own: without the app it takes no teammates' work.
teamAccountsIdle: !github,
...(github ? { pushOrigin: projectPushOrigin(github.app), branchPushOrigin: projectBranchOrigin(github.app) } : {}),
// When a moved rule's task merged, as GitHub dates it, read at the instructions save (superTabs/ruleMoves.ts, task #3463).
...(github ? { pullRequestMerges: (_env: string, _tab: string, _project: string, urls: string[]) => appPullRequestMerges(github.app)(urls) } : {}),
// A task's Ready reads the files its pull requests change, and the server opens its review steps from them (task #3401).
...(github ? { pullRequestFiles: projectPullRequestFiles(github.app) } : {}),
// A verdict naming a head the record does not have, and a Ready, read the pull request's head now (task #3628).
...(github ? { pullRequestReader: () => appPullRequestWatch(github.app) } : {}),
// A start or assignment its account cannot reach the repository from is refused at once, by the read Project details
// shows (task #3266): the token endpoint below would refuse it on the machine, after the start was queued.
...(github ? { repositoryReach: async (autonomousEnv: string, tabId: string, projectId: string, repositoryUrl: string) =>
repositoryReachOf(await projectGitHubAccess(github.app, repositoryUrl, { canApprove: false,
approved: async repository => !teamAccountRequired() || approvals.approved(autonomousEnv, tabId, projectId, repository) })) } : {}),
// Every server answers the quiet delivery poll from any server's full one, and tells the agent's machine of new work
// through whichever server holds its daemon socket (stability roadmap 2.4). Both only ever drop, never fail, on Redis.
polls: redisPollMemory(bus.pub),
// Each person is tried for joining by domain once per 2 minutes across every server (superTabs/autoJoin.ts).
autoJoins: redisAutoJoinMemory(bus.pub),
// What each machine's full report saw, so its heartbeats skip the Swarm load, and one sweep clock per Swarm (DB 2.5).
reports: redisReportMemory(bus.pub),
deliveriesWaiting: announceSwarmDeliveries, terminalWaiting: announceSwarmTerminal,
// And each machine whose host read a commit changed, so hosts read their Swarm changes only when told (WS Phase 2a).
hostsChanged: announceSwarmHostChanges,
// And each person in the Swarm, so their apps read it when told instead of every 3–15 s (WS Phase 2a).
swarmChanged: announceSwarmChanges,
// The host's answer often lands on another instance than the sender's wait.
terminalAnswered: requestId => { void bus.publishTerminalAnswered(requestId) },
watchTerminal: (requestId, onChange) => bus.subscribeTerminalAnswered(requestId, onChange),
// Its observer sockets may be on any instance.
accessChanged: tabId => { void bus.publishSwarmAccessChanged(tabId) },
sessionsConfidential: sessions => { void tellSessionViewers(sessions) },
// Chat voice messages are transcribed as the dial's and the phone's voice input are (routes/voice.ts), on the same daily
// quota of the person's account.
voice: {
reserve: (user, id, durationMs) => voiceQuotaService.reserveForUser(user.sub, user.autonomousEnv ?? 'prod', id, durationMs)
.then(() => 'ok' as const, error => { if (error instanceof VoiceQuotaExceededError) return 'limit' as const; throw error }),
transcribe: (wav, lang) => transcribeWavWithDeepgram(wav, 'audio/wav', pickWavLang(lang)),
commit: id => voiceQuotaService.commit(id),
release: id => voiceQuotaService.release(id).then(() => undefined),
} })
}
export async function superTabRoutes(app: FastifyInstance, options: { store?: SuperTabStore; github?: GitHubTokens | null; reads?: SwarmReads; hosts?: HostReads;
approvals?: Approvals; signIn?: GitHubSignIn | null } = {}) {
const github = options.github !== undefined ? options.github : await githubTokensFromEnv(env)
// Which repositories each Swarm may use the app on, and the app's sign-in that approves one (github/approvals.ts).
const approvals = options.approvals ?? new PrismaApprovals(prisma)
const signIn = options.signIn !== undefined ? options.signIn : githubSignIn(env)
const approvalSecret = github ? stateSecret(github.app.config) : null
const createSecret = github ? createStateSecret(github.app.config) : null
// No token for a repository nobody who can push to it approved for this project: a project names its repository as
// text, and the app is on other people's repositories too.
// Rolled out with the team account (machines.ts `teamAccountRequired`): a Swarm outside it gets tokens as before, so a
// bug here cannot stop other people's Swarms.
const requireApproval = async (user: { autonomousEnv: string }, grant: { tabId: string; projectId: string; approvalRequired: boolean },
repository: GitHubRepository) => {
if (!grant.approvalRequired) return
const { tabId, projectId } = grant
if (await approvals.approved(user.autonomousEnv, tabId, projectId, repository.fullName)) return
throw new SuperTabError('GITHUB_REPOSITORY_NOT_APPROVED', `Nobody who can push to ${repository.fullName} has approved it for this project yet, `
+ 'so teammates\' agents get no access to it. Someone who can push to it approves it in Harness: Project details, GitHub access.', 403)
}
const store = options.store ?? await productionStore(github, approvals)
// A rename in Machines reaches the Swarms the machine is in at once (MachineService.rename).
onMachineRenamed(rename => store.machineRenamed(rename))
// Another account's sign-in on a computer moves the computer's Swarm entries to it (computerMoves.ts).
onComputerSignedIn(signIn => store.computerSignedIn(signIn))
const reads = options.reads ?? new SwarmReads(store, { githubApp: !!github })
const hosts = options.hosts ?? new HostReads(store, { githubApp: !!github, teamAccountsIdle: !github })
// Members' apps read a project's GitHub access again when its revision moves (projects.ts githubRevision). A move that
// fails is logged and changes no answer: the apps then read again within 5 minutes, as before.
/** One organization check against GitHub for [userId], within OWNER_CHECKS_PER_MINUTE (security reviews of #3201). */
const limitOwnerChecks = async (userId: string) => {
if (!await ownerCheckAllowed(userId)) throw new SuperTabError('RATE_LIMITED', 'Too many organization checks. Try again in a minute.', 429)
}
const recorded = (tabId: string, move: () => Promise<unknown>) => move().catch(error => {
logger.warn('[github] access change not recorded', { tabId, error: String(error) })
})
app.get('/api/super-tabs/grids', (req, reply) => respond(reply, () => store.hostGrids(req.user!)))
// Each row says whether this server has the Harness GitHub App, so the host knows to ask for a start's token below.
app.get('/api/super-tabs/hosts/:machineId/pools', updateRequired)
// Replaces the three host reads above and below: every Swarm of the machine with `full=1` (at start and at least every
// 60 s), else only those with a change that concerns it since the cursor (design 4.10).
app.get<{ Params: z.infer<typeof hostParams>; Querystring: z.infer<typeof hostChangesQuery> }>('/api/super-tabs/hosts/:machineId/changes',
{ preHandler: [validateParams(hostParams), validateQuery(hostChangesQuery)] },
(req, reply) => respond(reply, () => hosts.changes(req.user!, req.params.machineId, { cursor: req.query.cursor, full: req.query.full === '1' })))
// The same read for a host that holds etags (`held`, design 4.10): too many for a URL, so in a body.
app.post<{ Params: z.infer<typeof hostParams>; Body: z.infer<typeof hostChangesBody> }>('/api/super-tabs/hosts/:machineId/changes',
{ bodyLimit: HOT_BODY_LIMIT, preHandler: [validateParams(hostParams), validateBody(hostChangesBody)] },
(req, reply) => respond(reply, () => hosts.changes(req.user!, req.params.machineId, req.body)))
// A read-only token for a queued start's project repository, for its host to clone and fetch the folder before the agent
// exists, so that the machine owner's own git login is never used for another person's project (docs/swarm-github-app.md).
// The machine owner's own project answers `github: false` and the host uses its own login, unless it asks `always` (a
// Harness Team account, which has none).
app.post<{ Params: z.infer<typeof hostStartParams>; Body: z.infer<typeof githubStartToken> }>('/api/super-tabs/hosts/:machineId/starts/:tabId/:startId/github-token',
{ preHandler: [validateParams(hostStartParams), validateBody(githubStartToken)] }, (req, reply) => respond(reply, async () => {
if (!github) throw new GitHubAppError('GITHUB_APP_NOT_CONFIGURED', 'This Harness server has no GitHub App configured.', 503)
const grant = await store.githubStart(req.user!, req.params.tabId, req.params.startId, req.params.machineId)
if (grant.own && req.body?.always !== true) return { github: false }
const repository = githubRepository(grant.repositoryUrl)
if (!repository) {
throw new SuperTabError('REPOSITORY_NOT_GITHUB', grant.repositoryUrl
? 'This project\'s repository is not on github.com, so the Harness GitHub App cannot give a token for it.'
: 'This project has no repository address yet. Its owner or Lead can share it (project.repository).', 409)
}
await requireApproval(req.user!, grant, repository)
return { github: true, ...await github.prepare(repository), requestedBy: grant.requester }
}))
app.get<{ Params: z.infer<typeof hostStartParams> }>('/api/super-tabs/hosts/:machineId/starts/:tabId/:startId', { preHandler: validateParams(hostStartParams) },
(req, reply) => respond(reply, () => store.start(req.user!, req.params.tabId, req.params.startId, undefined, req.params.machineId)))
app.post<{ Params: z.infer<typeof hostStartParams>; Body: z.infer<typeof operationSchema> }>('/api/super-tabs/hosts/:machineId/starts/:tabId/:startId',
{ preHandler: [validateParams(hostStartParams), validateBody(operationSchema)] }, (req, reply) => respond(reply, async () => {
if (!('startId' in req.body.command) || req.body.command.startId !== req.params.startId) throw new SuperTabError('FORBIDDEN', 'The host request must name this start.', 403)
return store.execute(req.user!, req.params.tabId, req.body, undefined, req.params.machineId, declaredHttpClient(req.headers))
}))
// Whether teammates' agents can push to a project's repository: the Harness GitHub App installed there
// (github/projectAccess.ts). `refresh=1`: a person installed it just now and checks again.
app.get<{ Params: z.infer<typeof projectParams>; Querystring: z.infer<typeof refreshQuery> }>('/api/super-tabs/:tabId/projects/:projectId/github',
{ preHandler: [validateParams(projectParams), validateQuery(refreshQuery)] },
(req, reply) => respond(reply, async () => {
const project = await store.projectRepository(req.user!, req.params.tabId, req.params.projectId)
const access = await projectGitHubAccess(github?.app ?? null, project.repositoryUrl, { canApprove: !!signIn,
approved: async repository => !project.approvalRequired || approvals.approved(req.user!.autonomousEnv, req.params.tabId, req.params.projectId, repository),
approvedFor: async repository => {
const ids = await approvals.approvedProjects(req.user!.autonomousEnv, req.params.tabId, repository)
return project.others.filter(other => ids.includes(other.id)).map(other => other.name)
},
you: repository => approvals.reader(req.user!.autonomousEnv, req.params.tabId, req.params.projectId, repository, req.user!.sub) },
req.query.refresh === '1')
// Only a person's check records what it found (opening GitHub access, Check again): every app reads again when the
// revision moves, on servers whose caches differ, and recording those reads would move it back and forth (review of
// #2713). A read without `refresh` may answer from an older cache, so it records nothing either.
if (req.query.refresh === '1' && req.query.record !== '0') await recorded(req.params.tabId, () => store.githubSeen(req.params.tabId, req.params.projectId, access.state))
return access
}))
// The jobs running and waiting on a project's GitHub Actions, for the route (github/ciLoad.ts): counts only, read through
// the app, by a person or an agent who may read the project. Only for a repository someone who can push to it approved for
// the project, in every Swarm, also where tokens ask no approval: otherwise a project could name another organisation's
// repository that has the app, read its counts and spend that installation's requests (security review 1 of #2428).
app.get<{ Params: z.infer<typeof projectParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/projects/:projectId/ci-load',
{ preHandler: [validateParams(projectParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, async () => {
const project = await store.ciRepository(req.user!, req.params.tabId, req.params.projectId, req.query.asAgentId)
return projectCiLoad(github?.app ?? null, project.repositoryUrl,
repository => approvals.approved(req.user!.autonomousEnv, req.params.tabId, req.params.projectId, repository))
}))
// Approving a project's repository: the person signs in to GitHub through the app (or installs it, which signs them in
// too), and GitHub sends them back to /api/github/callback with the state signed here for them, this project and scope.
app.post<{ Params: z.infer<typeof projectParams>; Body: z.infer<typeof approveBody> }>('/api/super-tabs/:tabId/projects/:projectId/github/approve',
{ preHandler: [validateParams(projectParams), validateBody(approveBody)] }, (req, reply) => respond(reply, async () => {
if (!github || !approvalSecret) throw new GitHubAppError('GITHUB_APP_NOT_CONFIGURED', 'This Harness server has no GitHub App configured.', 503)
if (!signIn) throw new GitHubAppError('GITHUB_APPROVAL_NOT_CONFIGURED', 'This Harness server cannot approve repositories yet: its administrator sets GITHUB_APP_CLIENT_ID and GITHUB_APP_CLIENT_SECRET.', 503)
const { repositoryUrl } = await store.projectRepository(req.user!, req.params.tabId, req.params.projectId)
const repository = githubRepository(repositoryUrl) ?? fail('REPOSITORY_NOT_GITHUB', 'This project\'s repository is not on github.com.', 409)
const state = signState(approvalSecret, { userId: req.user!.sub, autonomousEnv: req.user!.autonomousEnv, tabId: req.params.tabId,
projectId: req.params.projectId, repository: repository.fullName.toLowerCase(), scope: req.body.scope }, Date.now())
const install = github.app.installUrl()
return { authorizeUrl: signIn.authorizeUrl(state), installUrl: install ? `${install}?state=${encodeURIComponent(state)}` : null }
}))
// Whether an organization lets Harness create a repository there, for New Project's owner row before sign-in (F4, F5):
// 'not-configured' when this server can't (F6), so the apps hide the choice. A person of the Swarm only.
app.get<{ Params: z.infer<typeof ownerParams> }>('/api/super-tabs/:tabId/github/owners/:owner', { preHandler: validateParams(ownerParams) },
(req, reply) => respond(reply, async () => {
await store.requireWriter(req.user!, req.params.tabId)
if (!github || !signIn || !createSecret) return { state: 'not-configured' }
await limitOwnerChecks(req.user!.sub)
const readiness = await github.app.ownerReadiness(req.params.owner)
return readiness.state === 'not-installed' ? { state: readiness.state, ...(readiness.installUrl ? { installUrl: readiness.installUrl } : {}) } : { state: readiness.state }
}))
// A new repository for a project its owner just made: checked, recorded as waiting on the project, and the person sent to
// GitHub's sign-in, whose callback creates it with their own token (github/repositoryCreate.ts).
app.post<{ Params: z.infer<typeof projectParams>; Body: z.infer<typeof repositoryBody> }>('/api/super-tabs/:tabId/projects/:projectId/github/repository',
{ preHandler: [validateParams(projectParams), validateBody(repositoryBody)] }, (req, reply) => respond(reply, async () => {
if (!github || !createSecret) throw new GitHubAppError('GITHUB_APP_NOT_CONFIGURED', 'This Harness server has no GitHub App configured.', 503)
if (!signIn) throw new GitHubAppError('GITHUB_APPROVAL_NOT_CONFIGURED', 'This Harness server cannot create repositories yet: its administrator sets GITHUB_APP_CLIENT_ID and GITHUB_APP_CLIENT_SECRET.', 503)
const { owner, name } = req.body
const problem = repositoryNameProblem(name)
if (problem) throw new SuperTabError('REPOSITORY_NAME', problem, 400)
// Who may, before any call signed with the app's key (security review 1 of #3201).
await store.mayCreateRepository(req.user!, req.params.tabId, req.params.projectId)
await limitOwnerChecks(req.user!.sub)
const readiness = await github.app.ownerReadiness(owner)
if (readiness.state === 'not-installed') {
throw new GitHubAppError('GITHUB_APP_NOT_INSTALLED', `Harness isn't on ${owner}'s GitHub yet.`, 409, readiness.installUrl ? { installUrl: readiness.installUrl } : undefined)
}
if (readiness.state === 'not-accepted') {
throw new GitHubAppError('GITHUB_APP_PERMISSIONS', `An owner of ${owner} needs to accept Harness's new permission once: GitHub Settings → GitHub Apps → Harness.`, 409)
}
const at = Date.now()
await store.startRepositoryCreation(req.user!, req.params.tabId, req.params.projectId, { owner, name, private: req.body.private }, at)
const state = signCreateState(createSecret, { userId: req.user!.sub, autonomousEnv: req.user!.autonomousEnv, tabId: req.params.tabId,
projectId: req.params.projectId, owner, name, private: req.body.private, at }, at)
return { authorizeUrl: signIn.authorizeUrl(state), at }
}))
app.get<{ Querystring: z.infer<typeof callbackQuery> }>('/api/github/callback', { preHandler: validateQuery(callbackQuery) }, async (req, reply) => {
const page = (title: string, text: string, status = 200) => reply.status(status).type('text/html; charset=utf-8').send(githubPage(title, text))
const create = createSecret && github && req.query.state ? readCreateState(createSecret, req.query.state, Date.now()) : null
if (create && github) {
const githubApp = github.app
const expired = () => page('This link has expired', 'Go back to Harness and create the repository again.', 400)
const deleted = (made: string | null) => page('The project was deleted meanwhile',
made ? `${made} was made on GitHub, but no project uses it. You can go back to Harness.` : 'Nothing was created. You can go back to Harness.', 410)
if (!await store.isLive(create.tabId)) return expired()
// Claimed before the code is used: a replay, or the same link opened twice, finds nothing to claim (security review 2).
const claimed = await store.claimRepositoryAsk(create.tabId, create.projectId, create.userId, create.at)
if (claimed === 'gone') return deleted(null)
if (!claimed) return expired()
const finish = (outcome: Parameters<SuperTabStore['finishRepositoryCreation']>[4]) =>
store.finishRepositoryCreation(create.tabId, create.projectId, create.userId, create.at, outcome).catch(error => {
logger.warn('[github] repository creation outcome not recorded', { tabId: create.tabId, error: String(error) })
return 'error' as const
})
const full = `${create.owner}/${create.name}`
if (req.query.error || !req.query.code || !signIn) {
await finish({ state: 'cancelled' })
return page('Nothing was created', 'Go back to Harness and choose Try again.')
}
const outcome = await signIn.withUserToken(req.query.code, call => createRepository(call, githubApp, create)).catch(() => null)
if (!outcome || outcome.state === 'failed') {
await finish({ state: 'failed' })
return page('GitHub didn\'t answer', 'Nothing was created. Go back to Harness and choose Try again.', 502)
}
if (outcome.state === 'taken') {
await finish({ state: 'taken' })
return page(`${full} already exists`, 'Go back to Harness and choose another name.', 409)
}
if (outcome.state === 'refused') {
await finish({ state: 'refused', reason: outcome.reason })
return page(`GitHub didn't let you create a repository in ${create.owner}`, `${outcome.reason} Go back to Harness to choose another way.`, 403)
}
// Allowed for the project only once its outcome is recorded on it: a project deleted meanwhile, or an ask replaced,
// gets no approval (security review 1 of #3201). Its creator can push, so it is allowed as their sign-in for it would.
const written = await finish({ state: 'created', repositoryUrl: outcome.repositoryUrl, reachable: outcome.reachable })
if (written === 'gone') return deleted(outcome.repository)
if (written !== 'recorded') {
return page(`${outcome.repository} is made`, 'Harness could not record it on your project. Go back to Harness and choose Try again: it finds the repository you just made.', 500)
}
await approvals.approve({ userId: create.userId, autonomousEnv: create.autonomousEnv, tabId: create.tabId, projectId: create.projectId,
repository: outcome.repository.toLowerCase(), scope: 'project' }, outcome.login)
return page(`${outcome.repository} is ready`, outcome.reachable ? 'Go back to Harness: your project starts in it.'
: `Go back to Harness. Teammates' agents can use it once an owner of ${create.owner} adds it to Harness in GitHub: Settings → GitHub Apps → Harness → Configure.`)
}
const request = approvalSecret && req.query.state ? readState(approvalSecret, req.query.state, Date.now()) : null
if (!request) return page('This link has expired', 'Go back to Harness and choose Continue in GitHub again.', 400)
// A Swarm deleted since the link was made allows nothing, and says so as an expired link does (superTabs/deletion.ts).
if (!await store.isLive(request.tabId)) return page('This link has expired', 'Go back to Harness and choose Continue in GitHub again.', 400)
const named = githubRepository(`https://github.com/${request.repository}`)
// Cancel on GitHub's sign-in sends the person back with `error` and no code: they were told Harness was installed
// (local Team account run, 2026-10-28).
if (req.query.error) return page('Sign-in was cancelled', 'Nothing was allowed. Go back to Harness and choose Continue in GitHub to try again.')
if (!req.query.code || !signIn || !named) {
// Installed just now: this server stops keeping "not installed", and members' apps read again (task #2316).
if (named && github) {
github.app.forget(named)
await recorded(request.tabId, () => store.githubChanged(request.tabId, request.repository))
}
return page('Harness is installed', 'Go back to Harness and choose Continue in GitHub, so this project can use it.')
}
const seen = await signIn.permission(req.query.code, named).catch(() => null)
if (!seen) return page('GitHub could not confirm your access', 'Go back to Harness and choose Continue in GitHub again.', 502)
if (!seen.push) {
// Kept, so the app asks them to send it to someone who can rather than offering this sign-in again.
await approvals.refuse(request, seen.login).catch(() => undefined)
return page(`You can't push to ${named.fullName}`, 'So you can\'t allow it. Ask someone who can push to it to open the project in Harness and choose Set up GitHub for teammates.', 403)
}
await approvals.approve(request, seen.login)
await recorded(request.tabId, () => store.githubChanged(request.tabId, request.repository))
// Named, so the person knows which project it was for (local Team account run, 2026-10-28: 'This project can now use it').
const where = request.scope === 'swarm' ? 'every project in this Swarm'
: await store.projectNameFor(request.userId, request.autonomousEnv as AuthUser['autonomousEnv'], request.tabId, request.projectId).catch(() => null) ?? 'this project'
// Worded as the desktop's GitHub access dialog is (design 2026-10-30): allowed, not approved.
return page(`${named.fullName} is allowed`, `Teammates' agents in ${where} can now push to it. You can go back to Harness.`)
})
// A machine's readings for all of its Swarms in one call, answered per Swarm (DB roadmap 2.5).
app.post<{ Params: z.infer<typeof hostParams>; Body: z.infer<typeof readingsBody> }>('/api/super-tabs/hosts/:machineId/readings',
{ bodyLimit: READINGS_BODY_LIMIT, preHandler: [validateParams(hostParams), validateBody(readingsBody)] },
(req, reply) => respond(reply, async () => ({ results: (await store.observeMachines(req.user!, req.params.machineId, req.body.readings))
.map(({ tabId, error }) => error === undefined ? { tabId, ok: true as const } : { tabId, error: readingError(tabId, error) }) })))
// Retire no earlier than Dec 4, 2026 and 30 days after the first v2 Desktop, phone and CLI releases.
app.get<{ Params: z.infer<typeof machineParams> }>('/api/super-tabs/:tabId/machines/:machineId/removal', { preHandler: validateParams(machineParams) },
(req, reply) => respond(reply, () => store.machineRemoval(req.user!, req.params.tabId, req.params.machineId)))
app.get<{ Params: z.infer<typeof machineParams> }>('/api/super-tabs/:tabId/machines/:machineId/removal/v2', { preHandler: validateParams(machineParams) },
(req, reply) => respond(reply, () => store.machineRemoval(req.user!, req.params.tabId, req.params.machineId, 'v2')))
app.post<{ Params: z.infer<typeof machineParams>; Body: z.infer<typeof machineObservationSchema> }>('/api/super-tabs/:tabId/machines/:machineId',
{ preHandler: [validateParams(machineParams), validateBody(machineObservationSchema)] },
(req, reply) => respond(reply, () => store.observeMachine(req.user!, req.params.tabId, req.params.machineId, req.body)))
app.post<{ Params: z.infer<typeof tabParams>; Body: z.infer<typeof openNativeSchema> }>('/api/super-tabs/:tabId/native',
{ bodyLimit: HOT_BODY_LIMIT, preHandler: [validateParams(tabParams), validateBody(openNativeSchema)] },
(req, reply) => respond(reply, () => store.native.open(req.user!, req.params.tabId, req.body)))
app.get<{ Params: z.infer<typeof nativeParams> }>('/api/super-tabs/:tabId/native/:contextId',
{ preHandler: validateParams(nativeParams) },
(req, reply) => respond(reply, () => store.native.read(req.user!, req.params.tabId, req.params.contextId)))
app.post<{ Params: z.infer<typeof nativeParams>; Body: z.infer<typeof nativeEventSchema> }>('/api/super-tabs/:tabId/native/:contextId',
{ bodyLimit: HOT_BODY_LIMIT, preHandler: [validateParams(nativeParams), validateBody(nativeEventSchema)] },
(req, reply) => respond(reply, () => store.native.append(req.user!, req.params.tabId, req.params.contextId, req.body).catch(busyOnDuplicate)))
app.get('/api/super-tabs/routing-policy', (req, reply) => respond(reply, () => store.accountPolicy(req.user!)))
app.post<{ Body: z.infer<typeof savePolicy> }>('/api/super-tabs/routing-policy', { preHandler: validateBody(savePolicy) },
(req, reply) => respond(reply, () => store.saveAccountPolicy(req.user!, req.body.revision, req.body.policy)))
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/routing-policy',
{ preHandler: [validateParams(tabParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.tabPolicy(req.user!, req.params.tabId, req.query.asAgentId)))
app.get('/api/super-tabs', updateRequired)
app.get<{ Querystring: z.infer<typeof listQuery> }>('/api/super-tabs/list', { preHandler: validateQuery(listQuery) },
(req, reply) => respond(reply, () => reads.list(req.user!, req.query.after, req.query.mentions === '1')))
app.post<{ Body: z.infer<typeof create> }>('/api/super-tabs', { preHandler: validateBody(create) },
(req, reply) => respond(reply, () => store.create(req.user!, req.body.operationId, req.body.name, req.body.projectVisibility, req.body.workspaceId)))
// Workspaces (docs/plans/2026-10-27-swarm-workspaces.md): the person's Workspaces and the Swarms they may see in each.
// The owner's deleted Swarms they can still restore (superTabs/deletion.ts); restoring is the swarm.restore command.
app.get('/api/super-tabs/deleted', (req, reply) => respond(reply, () => store.deletedList(req.user!)))
app.get('/api/super-tabs/workspaces', (req, reply) => respond(reply, () => store.workspaceList(req.user!)))
app.post<{ Body: z.infer<typeof workspaceCreate> }>('/api/super-tabs/workspaces', { preHandler: validateBody(workspaceCreate) },
(req, reply) => respond(reply, () => { personOnly(req.user!); return store.workspaces.create(req.user!, req.body.operationId, req.body.name) }))
app.get('/api/super-tabs/workspace-invitations', (req, reply) => respond(reply, () => { personOnly(req.user!); return store.workspaces.invitationsFor(req.user!) }))
app.post<{ Params: z.infer<typeof workspaceParams>; Body: z.infer<typeof workspaceRename> }>('/api/super-tabs/workspaces/:workspaceId',
{ preHandler: [validateParams(workspaceParams), validateBody(workspaceRename)] },
(req, reply) => respond(reply, () => { personOnly(req.user!); return store.workspaces.rename(req.user!, req.params.workspaceId, req.body.name) }))
app.get<{ Params: z.infer<typeof workspaceParams> }>('/api/super-tabs/workspaces/:workspaceId/members', { preHandler: validateParams(workspaceParams) },
(req, reply) => respond(reply, () => { personOnly(req.user!); return store.workspaces.members(req.user!, req.params.workspaceId) }))
app.post<{ Params: z.infer<typeof workspaceParams>; Body: z.infer<typeof workspaceInvite> }>('/api/super-tabs/workspaces/:workspaceId/invitations',
{ preHandler: [validateParams(workspaceParams), validateBody(workspaceInvite)] },
(req, reply) => respond(reply, () => { personOnly(req.user!); return store.workspaces.invite(req.user!, req.params.workspaceId, req.body.email, req.body.role) }))
// A POST, not a DELETE, for the daemon (below).
app.post<{ Params: z.infer<typeof workspaceParams>; Body: z.infer<typeof workspaceRevoke> }>('/api/super-tabs/workspaces/:workspaceId/invitations/revoke',
{ preHandler: [validateParams(workspaceParams), validateBody(workspaceRevoke)] },
(req, reply) => respond(reply, () => { personOnly(req.user!); return store.workspaces.revokeInvitation(req.user!, req.params.workspaceId, req.body.email) }))
app.post<{ Params: z.infer<typeof workspaceParams> }>('/api/super-tabs/workspaces/:workspaceId/accept', { preHandler: validateParams(workspaceParams) },
(req, reply) => respond(reply, () => { personOnly(req.user!); return store.workspaces.accept(req.user!, req.params.workspaceId) }))
app.post<{ Params: z.infer<typeof workspaceMemberParams>; Body: z.infer<typeof workspaceRole> }>('/api/super-tabs/workspaces/:workspaceId/members/:userId',
{ preHandler: [validateParams(workspaceMemberParams), validateBody(workspaceRole)] },
(req, reply) => respond(reply, () => { personOnly(req.user!); return store.workspaces.setRole(req.user!, req.params.workspaceId, req.params.userId, req.body.role) }))
// A POST, not a DELETE: Desktop reaches the backend through the daemon, which forwards only GET and POST (cli superTabs/service.ts).
app.post<{ Params: z.infer<typeof workspaceMemberParams> }>('/api/super-tabs/workspaces/:workspaceId/members/:userId/remove', { preHandler: validateParams(workspaceMemberParams) },
(req, reply) => respond(reply, () => { personOnly(req.user!); return store.workspaces.remove(req.user!, req.params.workspaceId, req.params.userId) }))
app.post<{ Params: z.infer<typeof workspaceParams>; Body: z.infer<typeof workspaceTransfer> }>('/api/super-tabs/workspaces/:workspaceId/transfer',
{ preHandler: [validateParams(workspaceParams), validateBody(workspaceTransfer)] },
(req, reply) => respond(reply, () => { personOnly(req.user!); return store.workspaces.transfer(req.user!, req.params.workspaceId, req.body.userId) }))
app.post<{ Params: z.infer<typeof tabParams>; Body: z.infer<typeof workspaceVisibility> }>('/api/super-tabs/:tabId/workspace-visibility',
{ preHandler: [validateParams(tabParams), validateBody(workspaceVisibility)] },
(req, reply) => respond(reply, () => store.setWorkspaceVisibility(req.user!, req.params.tabId, req.body.visibility)))
app.post<{ Params: z.infer<typeof tabParams>; Body: z.infer<typeof workspaceJoin> }>('/api/super-tabs/:tabId/workspace-join',
{ preHandler: [validateParams(tabParams), validateBody(workspaceJoin)] },
(req, reply) => respond(reply, () => store.workspaceJoin(req.user!, req.params.tabId, req.body.operationId)))
app.get('/api/super-tabs/invitations', (req, reply) => respond(reply, () => store.invitations(req.user!)))
app.get<{ Params: z.infer<typeof tabParams> }>('/api/super-tabs/:tabId/sharing',
{ preHandler: validateParams(tabParams) },
(req, reply) => respond(reply, () => store.sharing(req.user!, req.params.tabId)))
app.post<{ Params: z.infer<typeof tabParams>; Body: z.infer<typeof saveSharingSchema> }>('/api/super-tabs/:tabId/sharing',
{ preHandler: [validateParams(tabParams), validateBody(saveSharingSchema)] },
(req, reply) => respond(reply, () => store.saveSharing(req.user!, req.params.tabId, req.body)))
// Preview uses a body so bearer link tokens never enter HTTP access/error logs.
app.post<{ Params: z.infer<typeof tabParams>; Body: z.infer<typeof previewJoinSchema> }>('/api/super-tabs/:tabId/join/preview',
{ preHandler: [validateParams(tabParams), validateBody(previewJoinSchema)] },
(req, reply) => respond(reply, () => store.previewJoin(req.user!, req.params.tabId, req.body.token)))
app.post<{ Params: z.infer<typeof tabParams>; Body: z.infer<typeof joinSwarmSchema> }>('/api/super-tabs/:tabId/join',
{ preHandler: [validateParams(tabParams), validateBody(joinSwarmSchema)] },
(req, reply) => respond(reply, () => store.join(req.user!, req.params.tabId, req.body)))
app.get('/api/super-tabs/hosts/:machineId', updateRequired)
app.post<{ Params: z.infer<typeof hostParams> }>('/api/super-tabs/hosts/:machineId/terminal', { preHandler: validateParams(hostParams) },
(req, reply) => respond(reply, noting(req, () => store.hostTerminal(req.user!, req.params.machineId), picked => !picked.length)))
// Long-polls about 20 seconds; a request still waiting then answers `pending` and is read again.
app.get<{ Params: z.infer<typeof terminalParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/terminal/:requestId',
{ preHandler: [validateParams(terminalParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.terminalResult(req.user!, req.params.tabId, req.params.requestId, req.query.asAgentId, 20_000)))
app.get('/api/super-tabs/hosts/:machineId/assignments', updateRequired)
app.get('/api/super-tabs/:tabId', updateRequired)
// The read API by parts (design section 4, superTabs/read/contract.ts). `githubApp` on a first load (only when the server
// has the app) lets a Lead's start wire its token into another person's work (cli/src/superTabs/start.ts).
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/parts',
{ preHandler: [validateParams(tabParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => reads.firstLoad(req.user!, req.params.tabId, req.query.asAgentId)))
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof changesQuery> }>('/api/super-tabs/:tabId/changes',
{ preHandler: [validateParams(tabParams), validateQuery(changesQuery)] },
(req, reply) => respond(reply, noting(req, () => reads.changes(req.user!, req.params.tabId, req.query.cursor, req.query.asAgentId,
{ ...(req.query.machines === 'light' ? { lightMachines: true } : {}), ...(req.query.derivedEtag ? { derivedEtag: req.query.derivedEtag } : {}) }),
nothingChanged)))
// The whole readings on demand, for a client that takes lines in its changes (read/changes.ts).
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/machine-readings',
{ preHandler: [validateParams(tabParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => reads.machineReadings(req.user!, req.params.tabId, req.query.asAgentId)))
// A person's unread threads and each chat's latest message (read/inbox.ts): read when the change feed moves, never on a timer.
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof noQuery> }>('/api/super-tabs/:tabId/inbox',
{ preHandler: [validateParams(tabParams), validateQuery(noQuery)] }, async (req, reply) => {
const wait = await inboxWait(req.user!, req.params.tabId)
if (wait) return sendError(reply.header('Retry-After', String(wait)), 'Too many inbox reads. Try again in a moment.', 'RATE_LIMITED', 429)
return respond(reply, () => readInbox(store, req.user!, req.params.tabId))
})
// A person's followed threads, unread first (read/followedThreads.ts): Desktop's Threads view, read as the inbox is.
// `read`: how many read threads to answer (Activity's Threads asks 50 at a time, task #2835); 30 when not asked.
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof threadsQuery> }>('/api/super-tabs/:tabId/threads',
{ preHandler: [validateParams(tabParams), validateQuery(threadsQuery)] }, async (req, reply) => {
const wait = await threadsWait(req.user!, req.params.tabId)
if (wait) return sendError(reply.header('Retry-After', String(wait)), 'Too many thread reads. Try again in a moment.', 'RATE_LIMITED', 429)
return respond(reply, () => readFollowedThreads(store, req.user!, req.params.tabId, req.query.read))
})
// One project's Buddy proposals its PM has not answered, oldest first (superTabs/proposals.ts): the Tasks view's Proposals.
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof proposalsQuery> }>('/api/super-tabs/:tabId/proposals',
{ preHandler: [validateParams(tabParams), validateQuery(proposalsQuery)] },
(req, reply) => respond(reply, () => store.proposals(req.user!, req.params.tabId, req.query.projectId)))
// The projects a person may pick a main project from (superTabs/projectList.ts, task #4540), read when Cmd-N or Join opens.
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof projectListQuery> }>('/api/super-tabs/:tabId/project-list',
{ preHandler: [validateParams(tabParams), validateQuery(projectListQuery)] },
(req, reply) => respond(reply, () => store.projectList(req.user!, req.params.tabId, req.query.invitedTo)))
// A person's Activity (superTabs/activity.ts): replies to their messages and reactions to them, read when its digest moves.
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof noQuery> }>('/api/super-tabs/:tabId/activity',
{ preHandler: [validateParams(tabParams), validateQuery(noQuery)] }, async (req, reply) => {
const wait = await activityWait(req.user!, req.params.tabId)
if (wait) return sendError(reply.header('Retry-After', String(wait)), 'Too many Activity reads. Try again in a moment.', 'RATE_LIMITED', 429)
return respond(reply, () => reads.activity(req.user!, req.params.tabId))
})
// A person's due reminders and when the next is due (superTabs/reminders.ts), read when its digest moves or at that time.
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof noQuery> }>('/api/super-tabs/:tabId/reminders',
{ preHandler: [validateParams(tabParams), validateQuery(noQuery)] }, async (req, reply) => {
const wait = await reminderWait(req.user!, req.params.tabId)
if (wait) return sendError(reply.header('Retry-After', String(wait)), 'Too many reminder reads. Try again in a moment.', 'RATE_LIMITED', 429)
return respond(reply, () => reads.reminders(req.user!, req.params.tabId))
})
app.post<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof noQuery>; Body: z.infer<typeof activityReadBody> }>('/api/super-tabs/:tabId/activity/read',
{ preHandler: [validateParams(tabParams), validateQuery(noQuery), validateBody(activityReadBody)] },
(req, reply) => respond(reply, () => reads.markActivityRead(req.user!, req.params.tabId, req.body.through)))
// A project's daily scorecard (superTabs/scorecard), or every readable project's with scope=all.
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof scorecardQuery> }>('/api/super-tabs/:tabId/scorecard',
{ preHandler: [validateParams(tabParams), validateQuery(scorecardQuery)] },
(req, reply) => respond(reply, () => reads.scorecard(req.user!, req.params.tabId, req.query.projectId, req.query.scope === 'all')))
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof leaderboardQuery> }>('/api/super-tabs/:tabId/leaderboard',
{ preHandler: [validateParams(tabParams), validateQuery(leaderboardQuery)] },
(req, reply) => respond(reply, () => reads.leaderboard(req.user!, req.params.tabId, req.query.projectId, req.query.week, req.query.period, req.query.scope === 'all')))
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof personalTasksQuery> }>('/api/super-tabs/:tabId/personal-tasks',
{ preHandler: [validateParams(tabParams), validateQuery(personalTasksQuery)] },
(req, reply) => respond(reply, () => reads.personalTasks(req.user!, req.params.tabId, req.query.before)))
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof dormantQuery> }>('/api/super-tabs/:tabId/groups',
{ preHandler: [validateParams(tabParams), validateQuery(dormantQuery)] },
(req, reply) => respond(reply, () => reads.dormant(req.user!, req.params.tabId, { projectId: req.query.projectId, before: req.query.before }, req.query.asAgentId)))
app.get<{ Params: z.infer<typeof groupParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/groups/:conversationId',
{ preHandler: [validateParams(groupParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => reads.group(req.user!, req.params.tabId, req.params.conversationId, req.query.asAgentId)))
// A one-hour installation token for the repository of the calling agent's task, for git and gh on its machine
// (docs/swarm-github-app.md). Only that agent's host, on the machine running it, while the task is in its hands.
app.post<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof agentQuery>; Body: z.infer<typeof githubToken> }>('/api/super-tabs/:tabId/github-token',
{ preHandler: [validateParams(tabParams), validateQuery(agentQuery), validateBody(githubToken)] },
(req, reply) => respond(reply, async () => {
if (!github) throw new GitHubAppError('GITHUB_APP_NOT_CONFIGURED', 'This Harness server has no GitHub App configured.', 503)
const grant = await store.githubAssignment(req.user!, req.params.tabId, req.query.asAgentId, req.body.machineId)
const repository = githubRepository(grant.repositoryUrl)
if (!repository) {
throw new SuperTabError('REPOSITORY_NOT_GITHUB', grant.repositoryUrl
? 'This project\'s repository is not on github.com, so the Harness GitHub App cannot give a token for it.'
: 'This project has no repository address yet. Its owner or Lead can share it (project.repository).', 409)
}
await requireApproval(req.user!, grant, repository)
const issued = await github.issue({ tabId: grant.tabId, agentId: grant.agentId, assignmentId: grant.assignmentId, repository })
// A Harness Team account has no git identity of its own: its commits are the app's, as its pull requests are
// (github/app.ts commitIdentity). A person's machine keeps its owner's, as before.
const commitAs = isTeamSession(req.user) ? github.app.commitIdentity() : null
return { ...issued, ...attribution(grant), ...(commitAs ? { commitAs } : {}) }
}))
app.post<{ Params: z.infer<typeof tabParams>; Body: z.infer<typeof operation> }>('/api/super-tabs/:tabId/commands',
{ preHandler: [validateParams(tabParams), validateBody(operation, { named: true })] },
(req, reply) => respond(reply, () => store.execute(req.user!, req.params.tabId,
{ operationId: req.body.operationId, command: req.body.command }, req.body.asAgentId, undefined, declaredHttpClient(req.headers))))
// A chat file comes straight from the client (the desktop's and the browser's account token, or the daemon's for
// `post --attach`), never through a machine or the relay. Multipart is parsed in this scope alone, so no other
// Super Tab route's body handling changes.
await app.register(async scope => {
// One byte over the limit, so the store sees an oversized file and refuses it rather than keeping a cut one.
// The file may come gzipped, said by `contentEncoding: gzip` between the two (attachments.ts formFileBody): the limit
// here is on what arrives, and the store's cap on what it unpacks to.
await scope.register(multipart, { limits: { fileSize: MAX_FORM_ATTACHMENT_BYTES + 1, files: 1, fields: 2, parts: 3 } })
scope.post<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/attachments',
{ preHandler: [validateParams(tabParams), validateQuery(agentQuery)] }, (req, reply) => respond(reply, async () => {
let conversationId: string | null = null, encoding: 'gzip' | null | undefined, uploaded: unknown = null, stored: unknown = null
try {
for await (const part of req.parts()) {
if (part.type === 'field') {
if (part.fieldname === 'conversationId' && conversationId === null && encoding === undefined && !uploaded) {
conversationId = id.safeParse(part.value).data ?? fail('INVALID_ATTACHMENT', 'Name the conversation to attach the file to.', 400)
} else if (part.fieldname === 'contentEncoding' && conversationId !== null && encoding === undefined && !uploaded) {
encoding = part.value === 'gzip' ? 'gzip' : fail('INVALID_ATTACHMENT', 'Send the file gzipped or as it is.', 400)
} else fail('INVALID_ATTACHMENT', 'Send conversationId, then one file.', 400)
} else {
if (!conversationId || uploaded) { part.file.resume(); fail('INVALID_ATTACHMENT', 'Send conversationId, then one file.', 400) }
const body = formFileBody(part.file, encoding ?? null)
try {
uploaded = await store.upload(req.user!, req.params.tabId, conversationId,
{ name: part.filename, mime: part.mimetype, body }, req.query.asAgentId)
} catch (error) {
stored = unpackFailed(body) ? new SuperTabError('INVALID_ATTACHMENT', 'The file was not valid gzip.', 400) : error
throw stored
} finally { stopUnpacking(part.file, body); part.file.resume() }
}
}
} catch (error) {
// Only the form itself is the client's fault; the store's own failures keep their status.
const code = error && typeof error === 'object' && 'code' in error ? error.code : null
if (code === 'FST_REQ_FILE_TOO_LARGE') fail('ATTACHMENT_TOO_LARGE', FORM_TOO_LARGE_MESSAGE, 413)
if (error instanceof SuperTabError || (error === stored && !(code && String(code).startsWith('FST_')))) throw error
fail('INVALID_ATTACHMENT', 'Send a multipart form with conversationId and one file.', 400)
}
return uploaded ?? fail('INVALID_ATTACHMENT', 'Attach one file.', 400)
}))
})
// A file over the form's 10 MB goes straight to the bucket on a link this server signs, then is checked here before any
// message can carry it (store.ts uploadLink, completeUpload). Neither request carries the file, so both stay small.
app.post<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof agentQuery>; Body: z.infer<typeof uploadLinkBody> }>('/api/super-tabs/:tabId/attachments/uploads',
{ bodyLimit: 4096, preHandler: [validateParams(tabParams), validateQuery(agentQuery), validateBody(uploadLinkBody)] },
(req, reply) => respond(reply, () => store.uploadLink(req.user!, req.params.tabId, req.body.conversationId,
{ name: req.body.name, mime: req.body.mime, size: req.body.size }, req.query.asAgentId)))
app.post<{ Params: z.infer<typeof uploadParams>; Querystring: z.infer<typeof agentQuery>; Body: z.infer<typeof emptyBody> }>('/api/super-tabs/:tabId/attachments/uploads/:uploadId/complete',
{ bodyLimit: 1024, preHandler: [validateParams(uploadParams), validateQuery(agentQuery), validateBody(emptyBody)] },
(req, reply) => respond(reply, () => store.completeUpload(req.user!, req.params.tabId, req.params.uploadId, req.query.asAgentId)))
// A voice message's file, uploaded either way above, is read once before it is sent: its length, waveform and words (store.ts
// readVoice). Its answer carries no words; message.send makes them the message's text.
app.post<{ Params: z.infer<typeof voiceParams>; Querystring: z.infer<typeof voiceQuery>; Body: z.infer<typeof emptyBody> }>('/api/super-tabs/:tabId/attachments/:attachmentId/voice',
{ bodyLimit: 1024, preHandler: [validateParams(voiceParams), validateQuery(voiceQuery), validateBody(emptyBody)] },
(req, reply) => respond(reply, () => store.readVoice(req.user!, req.params.tabId, req.params.attachmentId, req.query.lang ?? 'en', req.query.asAgentId)))
// An avatar picture (superTabs/profile.ts) comes the same way, alone in its form; the command that sets it names whose it is.
await app.register(async scope => {
await scope.register(multipart, { limits: { fileSize: MAX_AVATAR_BYTES + 1, files: 1, fields: 0, parts: 1 } })
scope.post<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/avatars',
{ preHandler: [validateParams(tabParams), validateQuery(agentQuery)] }, (req, reply) => respond(reply, async () => {
let uploaded: unknown = null, stored: unknown = null
try {
for await (const part of req.parts()) {
if (part.type === 'field' || uploaded) { if (part.type === 'file') part.file.resume(); fail('INVALID_AVATAR', 'Send one picture.', 400) }
try { uploaded = await store.uploadAvatar(req.user!, req.params.tabId, { body: part.file }, req.query.asAgentId) }
catch (error) { stored = error; throw error } finally { part.file.resume() }
}
} catch (error) {
const code = error && typeof error === 'object' && 'code' in error ? error.code : null
if (code === 'FST_REQ_FILE_TOO_LARGE') fail('AVATAR_TOO_LARGE', 'An avatar can be at most 1 MB.', 413)
if (error instanceof SuperTabError || (error === stored && !(code && String(code).startsWith('FST_')))) throw error
fail('INVALID_AVATAR', 'Send a multipart form with one picture.', 400)
}
return uploaded ?? fail('INVALID_AVATAR', 'Send one picture.', 400)
}))
})
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof mentionQuery> }>('/api/super-tabs/:tabId/mentions',
{ preHandler: [validateParams(tabParams), validateQuery(mentionQuery)] },
(req, reply) => respond(reply, () => store.mentions(req.user!, req.params.tabId, req.query.state)))
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof personDecisionQuery> }>('/api/super-tabs/:tabId/decisions',
{ preHandler: [validateParams(tabParams), validateQuery(personDecisionQuery)] },
(req, reply) => respond(reply, () => store.decisions(req.user!, req.params.tabId, req.query.state, req.query.asAgentId)))
app.get<{ Params: z.infer<typeof startParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/starts/:startId',
{ preHandler: [validateParams(startParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.start(req.user!, req.params.tabId, req.params.startId, req.query.asAgentId)))
app.get<{ Params: z.infer<typeof messageParams>; Querystring: z.infer<typeof messageQuery> }>('/api/super-tabs/:tabId/conversations/:conversationId/messages',
{ preHandler: [validateParams(messageParams), validateQuery(messageQuery)] },
(req, reply) => respond(reply, noting(req, () => store.messages(req.user!, req.params.tabId, req.params.conversationId, req.query, req.query.asAgentId),
page => req.query.after !== undefined && !page.messages.length)))
// A fresh short-lived link to one file of one message, for whoever may read that message now (superTabs/fileLinks.ts).
app.get<{ Params: z.infer<typeof fileParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/conversations/:conversationId/messages/:messageId/attachments/:attachmentId',
{ preHandler: [validateParams(fileParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.fileLink(req.user!, req.params.tabId, req.params.conversationId, req.params.messageId,
req.params.attachmentId, req.query.asAgentId)))
app.post<{ Params: z.infer<typeof messageParams>; Querystring: z.infer<typeof noQuery>; Body: z.infer<typeof readMarkBody> }>('/api/super-tabs/:tabId/conversations/:conversationId/read',
{ preHandler: [validateParams(messageParams), validateQuery(noQuery), validateBody(readMarkBody)] },
(req, reply) => respond(reply, () => reads.markRead(req.user!, req.params.tabId, req.params.conversationId, req.body.sequence)))
// A person's own Mark unread (superTabs/readMarks.ts lowerReadMark): the one write that moves their mark back.
app.post<{ Params: z.infer<typeof messageParams>; Querystring: z.infer<typeof noQuery>; Body: z.infer<typeof unreadBody> }>('/api/super-tabs/:tabId/conversations/:conversationId/unread',
{ preHandler: [validateParams(messageParams), validateQuery(noQuery), validateBody(unreadBody)] },
(req, reply) => respond(reply, () => reads.markUnread(req.user!, req.params.tabId, req.params.conversationId, req.body.sequence)))
// A person's own thread mark (superTabs/readMarks.ts), as the conversation's above.
app.post<{ Params: z.infer<typeof threadParams>; Querystring: z.infer<typeof noQuery>; Body: z.infer<typeof readMarkBody> }>('/api/super-tabs/:tabId/conversations/:conversationId/threads/:threadId/read',
{ preHandler: [validateParams(threadParams), validateQuery(noQuery), validateBody(readMarkBody)] },
(req, reply) => respond(reply, () => reads.markThreadRead(req.user!, req.params.tabId, req.params.conversationId, req.params.threadId, req.body.sequence)))
// A person's own Follow or Unfollow of a thread, kept on its thread mark (superTabs/readMarks.ts writeThreadFollow); null clears it.
app.post<{ Params: z.infer<typeof threadParams>; Querystring: z.infer<typeof noQuery>; Body: z.infer<typeof followBody> }>('/api/super-tabs/:tabId/conversations/:conversationId/threads/:threadId/follow',
{ preHandler: [validateParams(threadParams), validateQuery(noQuery), validateBody(followBody)] },
(req, reply) => respond(reply, () => reads.followThread(req.user!, req.params.tabId, req.params.conversationId, req.params.threadId, req.body.follow)))
// A person's own reminder of a message (superTabs/reminders.ts): set or replace its time, or clear it (`at` null).
app.post<{ Params: z.infer<typeof reminderParams>; Querystring: z.infer<typeof noQuery>; Body: z.infer<typeof reminderBody> }>('/api/super-tabs/:tabId/conversations/:conversationId/messages/:messageId/reminder',
{ preHandler: [validateParams(reminderParams), validateQuery(noQuery), validateBody(reminderBody)] },
(req, reply) => respond(reply, () => req.body.at === null
? reads.clearReminder(req.user!, req.params.tabId, req.params.conversationId, req.params.messageId)
: reads.setReminder(req.user!, req.params.tabId, req.params.conversationId, req.params.messageId, req.body.at)))
// A person's own notification setting for a chat (superTabs/chatSettings.ts): all, mentions or muted.
app.post<{ Params: z.infer<typeof messageParams>; Querystring: z.infer<typeof noQuery>; Body: z.infer<typeof notifyBody> }>('/api/super-tabs/:tabId/conversations/:conversationId/notifications',
{ preHandler: [validateParams(messageParams), validateQuery(noQuery), validateBody(notifyBody)] },
(req, reply) => respond(reply, () => reads.setChatNotify(req.user!, req.params.tabId, req.params.conversationId, req.body.notify)))
app.get<{ Params: z.infer<typeof messageParams>; Querystring: z.infer<typeof catchUpQuery> }>('/api/super-tabs/:tabId/conversations/:conversationId/catch-up',
{ preHandler: [validateParams(messageParams), validateQuery(catchUpQuery)] },
(req, reply) => respond(reply, () => store.catchUp(req.user!, req.params.tabId, req.params.conversationId, req.query.after)))
app.get<{ Params: z.infer<typeof messageParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/conversations/:conversationId/attempts',
{ preHandler: [validateParams(messageParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.attempts(req.user!, req.params.tabId, req.params.conversationId, req.query.asAgentId)))
// What routed attempts cost and how they went over a period (superTabs/costs/read.ts).
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof costsQuery> }>('/api/super-tabs/:tabId/costs',
{ preHandler: [validateParams(tabParams), validateQuery(costsQuery)] },
(req, reply) => respond(reply, () => store.costSummary(req.user!, req.params.tabId, req.query.days, req.query.asAgentId)))
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/route-decisions',
{ preHandler: [validateParams(tabParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.routeDecisions(req.user!, req.params.tabId, req.query.asAgentId)))
app.get<{ Params: z.infer<typeof roleParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/projects/:projectId/roles/:roleId',
{ preHandler: [validateParams(roleParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.roleText(req.user!, req.params.tabId, req.params.projectId, req.params.roleId, req.query.asAgentId)))
// A project's instructions, its kept revisions and one of them (superTabs/instructions.ts): read when the revision moves.
app.get<{ Params: z.infer<typeof projectParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/projects/:projectId/instructions',
{ preHandler: [validateParams(projectParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.instructions(req.user!, req.params.tabId, req.params.projectId, req.query.asAgentId)))
app.get<{ Params: z.infer<typeof projectParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/projects/:projectId/instructions/revisions',
{ preHandler: [validateParams(projectParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.instructionRevisions(req.user!, req.params.tabId, req.params.projectId, req.query.asAgentId)))
app.get<{ Params: z.infer<typeof revisionParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/projects/:projectId/instructions/revisions/:revision',
{ preHandler: [validateParams(revisionParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.instructionRevision(req.user!, req.params.tabId, req.params.projectId, req.params.revision, req.query.asAgentId)))
// A project's rules record and its kept revisions (superTabs/policy.ts): shown to people, in no prompt.
app.get<{ Params: z.infer<typeof projectParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/projects/:projectId/rules',
{ preHandler: [validateParams(projectParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.projectRules(req.user!, req.params.tabId, req.params.projectId, req.query.asAgentId)))
// Its machine rules alone, from the newest revision: what the route applies (task #3964).
app.get<{ Params: z.infer<typeof projectParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/projects/:projectId/machine-rules',
{ preHandler: [validateParams(projectParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.projectMachineRules(req.user!, req.params.tabId, req.params.projectId, req.query.asAgentId)))
app.get<{ Params: z.infer<typeof decisionParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/route-decisions/:decisionId',
{ preHandler: [validateParams(decisionParams), validateQuery(agentQuery)] },
(req, reply) => respond(reply, () => store.routeDecision(req.user!, req.params.tabId, req.params.decisionId, req.query.asAgentId)))
app.get<{ Params: z.infer<typeof deliveryParams>; Querystring: z.infer<typeof deliveryListQuery> }>('/api/super-tabs/:tabId/agents/:agentId/deliveries',
{ preHandler: [validateParams(deliveryParams), validateQuery(deliveryListQuery)] },
(req, reply) => respond(reply, noting(req, () => store.deliveries(req.user!, req.params.tabId, req.params.agentId, req.query.turn), open => !open.length)))
app.post<{ Params: z.infer<typeof deliveryActionParams>; Body: z.infer<typeof deliveryAction> }>('/api/super-tabs/:tabId/agents/:agentId/deliveries/:deliveryId',
{ bodyLimit: HOT_BODY_LIMIT, preHandler: [validateParams(deliveryActionParams), validateBody(deliveryAction)] },
(req, reply) => respond(reply, () => store.delivery(req.user!, req.params.tabId, req.params.agentId, req.params.deliveryId, req.body)))
app.get<{ Params: z.infer<typeof deliveryActionParams> }>('/api/super-tabs/:tabId/agents/:agentId/deliveries/:deliveryId',
{ preHandler: validateParams(deliveryActionParams) },
(req, reply) => respond(reply, () => store.deliveryReceipt(req.user!, req.params.tabId, req.params.agentId, req.params.deliveryId)))
app.post<{ Params: z.infer<typeof tabParams>; Body: z.infer<typeof stop> }>('/api/super-tabs/:tabId/stopped',
{ bodyLimit: HOT_BODY_LIMIT, preHandler: [validateParams(tabParams), validateBody(stop)] },
(req, reply) => respond(reply, () => store.stopped(req.user!, req.params.tabId, req.body.assignmentId, req.body.receipt)))
}