Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Merge batch 11-04 b0937: #2879 #2881 #2884 #2885 #2773 (#2886)

0xken152407b8f76bf7
diff --git a/backend/src/routes/agentRoute.test.ts b/backend/src/routes/agentRoute.test.ts
index 95289a465..0b78d2b08 100644
--- a/backend/src/routes/agentRoute.test.ts
+++ b/backend/src/routes/agentRoute.test.ts
@@ -4,7 +4,7 @@
// against the ids we actually offered, that the caller's clock wins over this endpoint's, and that an
// agent with no questions on record is described honestly in the prompt rather than being given
// somebody else's text.
-import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import Fastify, { type FastifyInstance, type InjectOptions } from 'fastify'
const authenticateAccessToken = vi.hoisted(() => vi.fn())
@@ -28,6 +28,8 @@ import { errorHandler } from '../middlewares/errorHandler.js'
import { registerAuthMiddleware, shouldSkipAuth } from '../middlewares/authMiddleware.js'
import { SsoAuthError } from '../lib/ssoAuth.js'
import { env } from '../config/env.js'
+import { logger } from '../utils/logger.js'
+import { useSharedRateCounter, windowKey } from '../lib/rateLimit.js'
const body = (over: Partial<RouteBody> = {}): RouteBody => ({
task: 'so sanh iphone vs samsung',
@@ -48,12 +50,12 @@ async function build(): Promise<FastifyInstance> {
return app
}
-function post(app: FastifyInstance, payload: unknown, opts: { noAuth?: boolean } = {}) {
+function post(app: FastifyInstance, payload: unknown, opts: { noAuth?: boolean; user?: string } = {}) {
const options: InjectOptions = {
method: 'POST',
url: AGENT_ROUTE_PATH,
payload: payload as InjectOptions['payload'],
- headers: opts.noAuth ? {} : { authorization: 'Bearer sso-token' },
+ headers: opts.noAuth ? {} : { authorization: `Bearer ${opts.user ?? 'sso-token'}` },
}
return app.inject(options)
}
@@ -155,6 +157,149 @@ describe('POST /api/route/agents', () => {
})
})
+// What one account may spend on Harness's model through this route. Before these bounds, each question
+// was unbounded (only the 1 MiB default body limit held), and an account could relay ~1 MB a call, 60
+// calls a minute, every day, out of the budget that also pays for ⌘B routing and Hub translation.
+describe('POST /api/route/agents: what one account may spend', () => {
+ const ranked = JSON.stringify({ ranking: [{ agentId: 'a1', score: 0.9, reason: 'phones' }, { agentId: 'a2', score: 0.1, reason: 'no' }] })
+ /** Each bearer token signs in as the account it names: `Bearer u7` is user u7. */
+ const asUser = () => authenticateAccessToken.mockImplementation(async (token: string) =>
+ ({ sub: token, email: `${token}@b.c`, role: 'user', autonomousEnv: 'prod' }))
+
+ beforeEach(() => {
+ vi.restoreAllMocks()
+ resetRouteRateLimits()
+ authenticateAccessToken.mockReset()
+ asUser()
+ env.CURSOR_LLM_BASE_URL = 'https://llm.example/v1'
+ env.CURSOR_LLM_API_KEY = 'llm-key'
+ })
+ afterEach(() => { vi.useRealTimers() })
+
+ it('refuses a question over 4,000 characters and never calls the model', async () => {
+ const fetchMock = llmSays(ranked)
+ vi.spyOn(globalThis, 'fetch').mockImplementation(fetchMock)
+ const app = await build()
+ const res = await post(app, body({
+ agents: [
+ { id: 'a1', name: 'Phones', prompts: ['x'.repeat(4_001)] },
+ { id: 'a2', name: 'Frontend', prompts: [] },
+ ],
+ }), { user: 'u1' })
+ expect(res.statusCode).toBe(400)
+ expect(fetchMock).not.toHaveBeenCalled()
+ })
+
+ it('still takes a question of exactly 4,000 characters', async () => {
+ vi.spyOn(globalThis, 'fetch').mockImplementation(llmSays(ranked))
+ const app = await build()
+ const res = await post(app, body({
+ agents: [
+ { id: 'a1', name: 'Phones', prompts: ['x'.repeat(4_000)] },
+ { id: 'a2', name: 'Frontend', prompts: [] },
+ ],
+ }), { user: 'u1' })
+ expect(res.statusCode).toBe(200)
+ })
+
+ it('refuses a body over the route limit with 413 before it is parsed', async () => {
+ // Every field inside its own bound, so only the body limit can refuse it: 15 agents with three
+ // 4,000-character questions in a three-byte script is ~540 KB of JSON.
+ const fetchMock = llmSays(ranked)
+ vi.spyOn(globalThis, 'fetch').mockImplementation(fetchMock)
+ const app = await build()
+ const agents = Array.from({ length: 15 }, (_, i) => ({ id: `a${i}`, name: 'n', prompts: Array(3).fill('ệ'.repeat(4_000)) }))
+ const payload = JSON.stringify({ task: 'route this', agents })
+ expect(Buffer.byteLength(payload)).toBeGreaterThan(300 * 1024)
+ const res = await app.inject({
+ method: 'POST', url: AGENT_ROUTE_PATH, payload,
+ headers: { authorization: 'Bearer u1', 'content-type': 'application/json' },
+ })
+ expect(res.statusCode).toBe(413)
+ expect(fetchMock).not.toHaveBeenCalled()
+ })
+
+ /**
+ * The shared count every pod uses (Redis in production), in memory: lets a test start an account or the
+ * whole day near its real limit instead of making a thousand calls.
+ */
+ const DAY_MS = 24 * 60 * 60_000
+ function sharedCounts(): Map<string, number> {
+ const counts = new Map<string, number>()
+ useSharedRateCounter(async (entries) => entries.map(({ key }) => {
+ const next = (counts.get(key) ?? 0) + 1
+ counts.set(key, next)
+ return next
+ }))
+ return counts
+ }
+ const accountDay = (user: string) => windowKey('agent-route-day', user, DAY_MS, Date.now())
+ const everyoneDay = () => windowKey('agent-route-all', 'all', DAY_MS, Date.now())
+ afterEach(() => { useSharedRateCounter(null) })
+
+ it('refuses an account past 1,000 routes a day with 429, in plain words, and counts each account on its own', async () => {
+ const counts = sharedCounts()
+ const fetchMock = llmSays(ranked)
+ vi.spyOn(globalThis, 'fetch').mockImplementation(fetchMock)
+ const app = await build()
+ counts.set(accountDay('u1'), 999)
+ expect((await post(app, body(), { user: 'u1' })).statusCode).toBe(200) // the 1,000th
+ const refused = await post(app, body(), { user: 'u1' })
+ expect(refused.statusCode).toBe(429)
+ expect(refused.json().error.message).toMatch(/today/i)
+ expect(fetchMock).toHaveBeenCalledTimes(1)
+ expect((await post(app, body(), { user: 'u2' })).statusCode).toBe(200)
+ })
+
+ it('gives an account its daily count back the next UTC day', async () => {
+ vi.useFakeTimers({ toFake: ['Date'] })
+ vi.setSystemTime(new Date('2026-11-03T23:59:00Z'))
+ const counts = sharedCounts()
+ vi.spyOn(globalThis, 'fetch').mockImplementation(llmSays(ranked))
+ const app = await build()
+ counts.set(accountDay('u1'), 1_000)
+ expect((await post(app, body(), { user: 'u1' })).statusCode).toBe(429)
+ vi.setSystemTime(new Date('2026-11-04T00:01:00Z'))
+ expect((await post(app, body(), { user: 'u1' })).statusCode).toBe(200)
+ })
+
+ it('holds the all-accounts ceiling of 100,000 a day, and an account refused on its own count does not spend it', async () => {
+ const counts = sharedCounts()
+ const fetchMock = llmSays(ranked)
+ vi.spyOn(globalThis, 'fetch').mockImplementation(fetchMock)
+ const app = await build()
+ counts.set(everyoneDay(), 99_999)
+ counts.set(accountDay('u9'), 1_000)
+ expect((await post(app, body(), { user: 'u9' })).statusCode).toBe(429) // its own count
+ expect(counts.get(everyoneDay())).toBe(99_999)
+ expect((await post(app, body(), { user: 'u1' })).statusCode).toBe(200) // the 100,000th
+ const over = await post(app, body(), { user: 'u2' })
+ expect(over.statusCode).toBe(503)
+ expect(over.json().error.message).toMatch(/today/i)
+ expect(fetchMock).toHaveBeenCalledTimes(1)
+ })
+
+ it('does not count the single-agent answer, which spends no call', async () => {
+ const counts = sharedCounts()
+ const app = await build()
+ counts.set(accountDay('u1'), 1_000)
+ const one = body({ agents: [{ id: 'a1', name: 'Phones', prompts: [] }] })
+ expect((await post(app, one, { user: 'u1' })).statusCode).toBe(200)
+ expect(counts.get(accountDay('u1'))).toBe(1_000)
+ })
+
+ it('logs the length of the prompt it sent, not only the task', async () => {
+ vi.spyOn(globalThis, 'fetch').mockImplementation(llmSays(ranked))
+ const info = vi.spyOn(logger, 'info')
+ const app = await build()
+ const sent = body({ agents: [{ id: 'a1', name: 'Phones', prompts: ['p'.repeat(3_000)] }, { id: 'a2', name: 'Frontend', prompts: [] }] })
+ expect((await post(app, sent, { user: 'u1' })).statusCode).toBe(200)
+ const line = info.mock.calls.find(([message]) => message === 'agent-route')
+ expect(line?.[1]).toMatchObject({ promptChars: buildRankPrompt(sent).length })
+ })
+})
+
+
describe('resolveBudgetMs', () => {
// The caller owns the clock: the CLI gives a spoken task 12s, and an endpoint that waits longer
// produces answers nobody is still listening for.
diff --git a/backend/src/routes/agentRoute.ts b/backend/src/routes/agentRoute.ts
index 168bddf4f..9ece80b96 100644
--- a/backend/src/routes/agentRoute.ts
+++ b/backend/src/routes/agentRoute.ts
@@ -53,18 +53,42 @@ const BUDGET_MS_RANGE = { min: 3_000, max: 20_000, fallback: 10_000 } as const
const RATE_LIMIT_WINDOW_MS = 60_000
const RATE_LIMIT_MAX = 60
+/**
+ * Per day, counted only for calls that reach the model. The minute cap alone let one account spend all
+ * day: 60 a minute is 86,400 calls. A person routing all day presses it a few hundred times; a loop does
+ * not get far. The all-accounts ceiling bounds what Harness pays here in a day, under the same budget as
+ * ⌘B routing and Hub translation. Past either, the CLI falls through to its own ladder (cli/src/lib/routeApi.ts,
+ * ending in pickAgentHeuristic), so routing degrades rather than stops. Both count in a UTC-day window shared through Redis (lib/rateLimit.ts).
+ */
+const DAY_MS = 24 * 60 * 60_000
+const PER_ACCOUNT_PER_DAY = 1_000
+const ALL_ACCOUNTS_PER_DAY = 100_000
+
+/**
+ * Bytes, enforced before the body is parsed: a request at what the CLI sends (15 agents, three questions
+ * stored at 1,000 characters, in a three-byte script such as Vietnamese: ~150 KB) passes with room to
+ * spare; a request packed to every field's own limit in such a script (~540 KB) does not, and needs not.
+ */
+const MAX_BODY_BYTES = 256 * 1024
+
const rateLimiter = createRateLimiter('agent-route', RATE_LIMIT_WINDOW_MS, RATE_LIMIT_MAX)
+const perAccountDay = createRateLimiter('agent-route-day', DAY_MS, PER_ACCOUNT_PER_DAY)
+const allAccountsDay = createRateLimiter('agent-route-all', DAY_MS, ALL_ACCOUNTS_PER_DAY)
export const routeRateLimitAllows = (userId: string, now = Date.now()): Promise<boolean> => rateLimiter.allows(userId, now)
/** Test seam: forgets this process' counts (lib/rateLimit.ts). */
-export const resetRouteRateLimits = (): void => rateLimiter.reset()
+export const resetRouteRateLimits = (): void => {
+ rateLimiter.reset()
+ perAccountDay.reset()
+ allAccountsDay.reset()
+}
const agentSchema = z.object({
id: z.string().min(1).max(128),
name: z.string().max(200).default(''),
/** The person's own last questions to this agent, newest first. Absent for a fresh agent. */
- prompts: z.array(z.string()).max(MAX_PROMPTS_PER_AGENT).default([]),
+ prompts: z.array(z.string().max(MAX_PROMPT_CHARS)).max(MAX_PROMPTS_PER_AGENT).default([]),
})
const routeBodySchema = z.object({
@@ -212,7 +236,7 @@ export function parseRanking(raw: string, offered: Set<string>): RankedRow[] | n
export async function agentRouteRoutes(app: FastifyInstance): Promise<void> {
app.post<{ Body: RouteBody }>(
AGENT_ROUTE_PATH,
- { preHandler: [validateBody(routeBodySchema)] },
+ { bodyLimit: MAX_BODY_BYTES, preHandler: [validateBody(routeBodySchema)] },
async (req, reply) => {
// Fail CLOSED on an unconfigured model, like the Cursor routes: an absent credential must never
// read as "answer some other way" on a path that spends credit.
@@ -236,12 +260,24 @@ export async function agentRouteRoutes(app: FastifyInstance): Promise<void> {
return
}
+ // The account's own count first, so calls refused for one account never spend everyone's ceiling.
+ if (!(await perAccountDay.allows(userId))) {
+ sendError(reply, "You've used today's routing limit. It resets at midnight UTC.", 'RATE_LIMITED', 429)
+ return
+ }
+ if (!(await allAccountsDay.allows('all'))) {
+ logger.error('agent-route: daily ceiling reached', { ceiling: ALL_ACCOUNTS_PER_DAY })
+ sendError(reply, "Harness's routing budget for today has run out. It resets at midnight UTC.", 'ROUTE_BUDGET', 503)
+ return
+ }
+
const budgetMs = resolveBudgetMs(body.budgetMs)
const offered = new Set(body.agents.map((a) => a.id))
const started = Date.now()
+ const prompt = buildRankPrompt(body)
try {
- const { text, model } = await callHarnessLlm(buildRankPrompt(body), budgetMs)
+ const { text, model } = await callHarnessLlm(prompt, budgetMs)
const ranking = parseRanking(text, offered)
if (!ranking) {
logger.error('agent-route: unusable answer', { model, userId, sample: text.slice(0, 200) })
@@ -255,6 +291,8 @@ export async function agentRouteRoutes(app: FastifyInstance): Promise<void> {
// state is the first thing to look at when a route reads wrong.
bare: body.agents.filter((a) => a.prompts.length === 0).length,
chars: body.task.length,
+ // What was paid for: the whole prompt, questions included, not only the task.
+ promptChars: prompt.length,
model,
ms: Date.now() - started,
top: ranking[0].score,
diff --git a/backend/src/superTabs/commands.ts b/backend/src/superTabs/commands.ts
index 6f8235556..df6e48306 100644
--- a/backend/src/superTabs/commands.ts
+++ b/backend/src/superTabs/commands.ts
@@ -3481,6 +3481,13 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,
}
// Its Reviewer's verdict asking for changes: its own review task waits for the new revision (liveness.ts waitingForRevision).
if (!command.passed && !byAssignee && actor.kind === 'agent') changesRequested(state, group, actor.agentId, context.now)
+ // A step's own agent failing its step's check asks its Engineer for changes the same way: a Tester's verdict has no set
+ // form for a post to carry, and its step waits for the new revision instead of moving after 30 quiet minutes (Ken,
+ // 2026-11-04: ~18 of 51 steps moved in two days were waiting on the Engineer's fix).
+ if (!command.passed && byAssignee && actor.kind === 'agent' && group.reviewOf) {
+ const build = state.conversations.find(c => c.id === group.reviewOf)
+ if (build) changesRequested(state, build, actor.agentId, context.now)
+ }
if (actor.kind === 'agent') renewOwnTask(state, group, actor.agentId, context.now)
break
}
diff --git a/backend/src/superTabs/instructions.test.ts b/backend/src/superTabs/instructions.test.ts
index f8199961c..b25f99d90 100644
--- a/backend/src/superTabs/instructions.test.ts
+++ b/backend/src/superTabs/instructions.test.ts
@@ -56,6 +56,15 @@ describe('the built-in template', () => {
})
// Ken, 2026-11-03: in two days agents sent openharness's PM 850 direct messages, 73% of them answers to the PM's own
// 1,437 (review asks, status asks, thanks), and Reviewers sent their verdicts to the PM instead of the Engineer.
+ // Ken, 2026-11-04: of 51 steps moved after 30 quiet minutes in two days, ~18 were Reviewers or Testers waiting on the
+ // Engineer's fix (their verdict reached the PM, or read "Changes needed", so liveness.ts toldEngineer never saw it) and ~7
+ // were Reviewers waiting for CI before giving a verdict that was ready.
+ it('has a changes verdict go to the Engineer in the form the Swarm reads, and Reviewers not wait for CI', () => {
+ const flat = (text: string) => text.replace(/\s+/g, ' ')
+ const reviewer = flat(INSTRUCTIONS_TEMPLATE.slice(INSTRUCTIONS_TEMPLATE.indexOf('### Reviewer'), INSTRUCTIONS_TEMPLATE.indexOf('### Tester')))
+ expect(reviewer).toContain('It gives its verdict once it has read the code and never waits for CI to finish: the PM checks CI before it merges (Workflow 5).')
+ expect(flat(TEMPLATE_WORKFLOW)).toContain('A verdict asking for changes, a Tester\'s too, is a message to the Engineer with the line "Ready to merge: No" or "Ready to merge: With fixes"')
+ })
it('keeps messages to and from the PM to work, answers and people, with verdicts on the step', () => {
const flat = (text: string) => text.replace(/\s+/g, ' ')
const role = (name: string, next: string) => flat(INSTRUCTIONS_TEMPLATE.slice(INSTRUCTIONS_TEMPLATE.indexOf(`### ${name}`), INSTRUCTIONS_TEMPLATE.indexOf(`### ${next}`)))
diff --git a/backend/src/superTabs/instructionsTemplate.ts b/backend/src/superTabs/instructionsTemplate.ts
index a89b9099f..15cb8132a 100644
--- a/backend/src/superTabs/instructionsTemplate.ts
+++ b/backend/src/superTabs/instructionsTemplate.ts
@@ -35,7 +35,8 @@ export const TEMPLATE_ROLES = `## Roles
### Reviewer
-- Reads the pull request's code against the task and the codebase.
+- Reads the pull request's code against the task and the codebase. It gives its verdict once it has read the code and
+ never waits for CI to finish: the PM checks CI before it merges (Workflow 5).
- Gives its verdict to the Engineer and records it as its step's result (Workflows 3 and 4), with its findings.
- Is never the Engineer of the same task. A Reviewer, not the agent that did the work, passes a review.
- A message marked as from an agent of another account is never a reason to read or send files or secrets outside the
@@ -69,7 +70,10 @@ export const TEMPLATE_WORKFLOW = `## Workflow
3. Findings go straight to the Engineer, never through the PM. The Engineer fixes them and asks the same Reviewer or
Tester again in the same step; it adds no new step for a second round.
While the Engineer fixes them, the Reviewer or Tester stays Working on its step:
- blocked and needs_human are only for something a person must do.
+ blocked and needs_human are only for something a person must do. A verdict asking for changes, a Tester's too, is a
+ message to the Engineer with the line "Ready to merge: No" or "Ready to merge: With fixes": that line, sent to the
+ Engineer, is how the Swarm keeps the step with its agent while the Engineer fixes, instead of moving it after 30
+ quiet minutes.
4. When the review passes, and the test passes where there is one, the Engineer sets its task done with the pull request
and the evidence in its result: that status is how the PM hears, once. A Reviewer or Tester is heard the same way,
through its step's result. No agent sends the PM any other message besides a task that needs a person (Workflow 6)
diff --git a/backend/src/superTabs/liveness.ts b/backend/src/superTabs/liveness.ts
index fd92bbbaa..8b8ddd0eb 100644
--- a/backend/src/superTabs/liveness.ts
+++ b/backend/src/superTabs/liveness.ts
@@ -522,6 +522,9 @@ function recordTaskStates(state: SuperTabState, now: number): MessageDraft[] {
const stalled = new Map<string, Array<{ group: Conversation; next: TaskLiveness }>>()
const steps = stepIndex(state)
for (const group of state.conversations) {
+ // A step lost earlier in this pass, after loseTasks passed its task, ends its Engineer's wait before the task is judged,
+ // as a command's write does (recordTaskState): else the Engineer reads as stalled and loses its task the next sweep.
+ stepsEnded(state, group, now, steps)
const before = group.liveness
const next = nextState(state, group, now, steps)
if (sameState(before, next)) continue
diff --git a/backend/src/superTabs/steps.test.ts b/backend/src/superTabs/steps.test.ts
index de8002f2f..5da0ae4b6 100644
--- a/backend/src/superTabs/steps.test.ts
+++ b/backend/src/superTabs/steps.test.ts
@@ -223,6 +223,29 @@ describe('assigning a step', () => {
expect(f.group(id).assignment).toMatchObject({ status: 'working' })
expect(f.group(id).assignment!.changesRequestedAt).toBeUndefined()
})
+
+ // Ken, 2026-11-04: ~18 of 51 steps moved after 30 quiet minutes in two days were a Reviewer or Tester waiting on the
+ // Engineer's fix. A Tester's verdict has no set form, and a step agent's failed check on its own step counted as the
+ // assignee's, so nothing marked the wait.
+ it('waits for the fix after a failed check on its own step, as after a failed check on the task', () => {
+ const f = stepsFixture()
+ const id = f.step('Test', f.as(f.dev), f.task, [{ description: 'Works offline on a Mac', independent: false }])
+ const tester = f.add('tester', id)
+ f.run({ type: 'work.assign', conversationId: id, agentId: tester }, f.as(f.dev))
+ const check = (passed: boolean) => f.run({ type: 'work.check', conversationId: id, assignmentId: f.group(id).assignment!.id,
+ checkId: f.group(id).checks[0]!.id, passed, evidence: passed ? 'Signs in offline' : 'Sign-in fails offline, steps below' } as Command, f.as(tester))
+ check(true)
+ expect(f.group(id).assignment!.changesRequestedAt).toBeUndefined()
+ check(false)
+ expect(f.group(id).assignment!.changesRequestedAt).toBe(f.now)
+ // The Engineer works on the fix (its own status renews only its own task), while the step stays quiet past 30 minutes.
+ f.tick(QUIET_RUNNING_MS + 5 * 60_000)
+ f.run({ type: 'work.status', conversationId: f.task, assignmentId: f.group(f.task).assignment!.id, status: 'working', result: 'Fixing the offline sign-in' } as Command, f.as(f.dev))
+ sweepLiveness(f.state, f.now)
+ expect(f.group(id).assignment).toMatchObject({ status: 'working' })
+ f.run({ type: 'work.status', conversationId: f.task, assignmentId: f.group(f.task).assignment!.id, status: 'review', result: 'PR 7, fixed' } as Command, f.as(f.dev))
+ expect(f.group(id).assignment!.changesRequestedAt).toBeUndefined()
+ })
})
describe('a step closes with its task', () => {
@@ -693,6 +716,27 @@ describe('an Engineer waiting on its own steps', () => {
})
}
+ // Diego, 2026-11-04 (#2578): a step lost inside the PM's poll left its task recorded as waiting on it, so the same poll woke
+ // the PM with a false stall and the Engineer lost its task 30 seconds later instead of after its 30 minutes.
+ it('gets its 30 minutes when its Reviewer goes quiet and the step is lost in the PM\'s poll, with no stall woken', () => {
+ const { f, id, reviewFor, task } = reviewing()
+ reviewFor(36)
+ f.tick(QUIET_RUNNING_MS + 5 * MIN)
+ const rows = sweepForLead(f.state, f.now)
+ expect(f.group(id).assignment).toMatchObject({ status: 'lost' })
+ expect(rows.filter(r => r.source?.kind === 'work-stalled')).toEqual([])
+ expect(task()).toMatchObject({ status: 'working' })
+ f.tick(30_000)
+ expect(sweepForLead(f.state, f.now).filter(r => r.source?.kind === 'work-stalled')).toEqual([])
+ expect(task()).toMatchObject({ status: 'working' })
+ f.tick(QUIET_RUNNING_MS - 30_000 - MIN)
+ sweepForLead(f.state, f.now)
+ expect(task()).toMatchObject({ status: 'working' })
+ f.tick(2 * MIN)
+ sweepForLead(f.state, f.now)
+ expect(task()).toMatchObject({ status: 'lost' })
+ })
+
it('a step in Ready waits on its task owner and never wakes the PM as a stalled review', () => {
const { f, id, stepStatus } = reviewing()
stepStatus('review')
diff --git a/mobile/test/swarm/swarm_conversation_test.dart b/mobile/test/swarm/swarm_conversation_test.dart
index c87153717..3de0396d9 100644
--- a/mobile/test/swarm/swarm_conversation_test.dart
+++ b/mobile/test/swarm/swarm_conversation_test.dart
@@ -1,5 +1,6 @@
import 'dart:async';
+import 'package:fake_async/fake_async.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness_mobile/swarm/model.dart';
import 'package:harness_mobile/swarm/swarm_conversation.dart';
@@ -1578,6 +1579,15 @@ void main() {
return (swarm: r.swarm, reader: r.reader, c: c);
}
+ /// What [opening] gives, on [async]'s clock: a timeout there passes only
+ /// when the test says so, never because the machine is slow.
+ T onFakeClock<T>(FakeAsync async, Future<T> opening) {
+ late final T opened;
+ unawaited(opening.then((value) => opened = value));
+ async.flushMicrotasks();
+ return opened;
+ }
+
/// The bodies [swarm] was sent, by receipt, each answered by hand.
Map<String, Completer<Object?>> held(FakeSwarm swarm) {
final answers = <String, Completer<Object?>>{};
@@ -1678,56 +1688,70 @@ void main() {
});
test('no answer in time says Not sent; Retry sends the same receipt, and '
- 'the first answer landing late still counts', () async {
- final r = await chat(timeout: const Duration(milliseconds: 20));
- final answers = held(r.swarm);
- r.c.draft = 'Once only';
-
- expect(await r.c.send(), isFalse);
- final post = r.c.outgoing.single;
- expect(post.state, SwarmPostState.unconfirmed);
- expect(r.c.unconfirmed, isTrue);
- expect(r.c.error, contains('not sent'));
- expect(r.c.canSend, isFalse, reason: 'the field is empty');
- r.c.draft = 'Meanwhile';
- expect(r.c.canSend, isTrue, reason: 'a lost post holds nothing back');
- r.c.draft = '';
-
- final again = r.c.retry(post);
- await settle();
- final posts = [
- for (final call in r.swarm.calls)
- if (call.path == commands) call.body!['operationId'],
- ];
- expect(posts, hasLength(2));
- expect(posts.toSet(), hasLength(1), reason: 'one post, one receipt');
- // Both tries are one command: its one answer answers both.
- answers.values.single.complete({'result': <String, dynamic>{}});
- expect(await again, isTrue);
- expect(post.state, SwarmPostState.confirmed);
- expect(r.c.error, isNull);
+ 'the first answer landing late still counts', () {
+ fakeAsync((async) {
+ final r = onFakeClock(async, chat());
+ final answers = held(r.swarm);
+ r.c.draft = 'Once only';
+
+ bool? sent;
+ unawaited(r.c.send().then((value) => sent = value));
+ async.elapse(SwarmOutbox.defaultTimeout);
+ expect(sent, isFalse);
+ final post = r.c.outgoing.single;
+ expect(post.state, SwarmPostState.unconfirmed);
+ expect(r.c.unconfirmed, isTrue);
+ expect(r.c.error, contains('not sent'));
+ expect(r.c.canSend, isFalse, reason: 'the field is empty');
+ r.c.draft = 'Meanwhile';
+ expect(r.c.canSend, isTrue, reason: 'a lost post holds nothing back');
+ r.c.draft = '';
+
+ bool? again;
+ unawaited(r.c.retry(post).then((value) => again = value));
+ async.flushMicrotasks();
+ final posts = [
+ for (final call in r.swarm.calls)
+ if (call.path == commands) call.body!['operationId'],
+ ];
+ expect(posts, hasLength(2));
+ expect(posts.toSet(), hasLength(1), reason: 'one post, one receipt');
+ // The last moment of the retry's own wait, whatever the machine's speed.
+ async.elapse(
+ SwarmOutbox.defaultTimeout - const Duration(milliseconds: 1),
+ );
+ expect(again, isNull);
+ // Both tries are one command: its one answer answers both.
+ answers.values.single.complete({'result': <String, dynamic>{}});
+ async.flushMicrotasks();
+ expect(again, isTrue);
+ expect(post.state, SwarmPostState.confirmed);
+ expect(r.c.error, isNull);
+ });
});
- test(
- 'a refusal that lands after Not sent still puts the text back',
- () async {
- final r = await chat(timeout: const Duration(milliseconds: 20));
+ test('a refusal that lands after Not sent still puts the text back', () {
+ fakeAsync((async) {
+ final r = onFakeClock(async, chat());
final answers = held(r.swarm);
r.c.draft = 'Too long';
- expect(await r.c.send(), isFalse);
+ bool? sent;
+ unawaited(r.c.send().then((value) => sent = value));
+ async.elapse(SwarmOutbox.defaultTimeout);
+ expect(sent, isFalse);
expect(r.c.outgoing.single.state, SwarmPostState.unconfirmed);
expect(r.c.draft, isEmpty);
answers.values.single.completeError(
const SuperTabFailure('TOO_LONG', 'Too long.', statusCode: 400),
);
- await settle();
+ async.flushMicrotasks();
expect(r.c.outgoing, isEmpty);
expect(r.c.draft, 'Too long');
expect(r.c.error, 'Too long.');
- },
- );
+ });
+ });
test('a refused post goes back to the draft while another stays on its '
'way', () async {
diff --git a/mobile/test/swarm/swarm_outbox_test.dart b/mobile/test/swarm/swarm_outbox_test.dart
index dbdecd84c..9bdc9864c 100644
--- a/mobile/test/swarm/swarm_outbox_test.dart
+++ b/mobile/test/swarm/swarm_outbox_test.dart
@@ -1,5 +1,6 @@
import 'dart:async';
+import 'package:fake_async/fake_async.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness_mobile/swarm/model.dart';
import 'package:harness_mobile/swarm/swarm_outbox.dart';
@@ -59,21 +60,31 @@ void main() {
);
test('a late failure of an earlier try never undoes the retry under way, '
- 'and the post is told taken once', () async {
- final r = rig(timeout: const Duration(milliseconds: 10));
- final p = post('op-1');
- expect(await r.box.add(p), SwarmPostOutcome.lost);
- final again = r.box.deliver(p);
- expect(p.state, SwarmPostState.sending);
-
- r.answers[0].completeError(StateError('reset'));
- await Future<void>.delayed(Duration.zero);
- expect(p.state, SwarmPostState.sending, reason: 'the retry speaks now');
-
- r.answers[1].complete({'result': <String, dynamic>{}});
- expect(await again, SwarmPostOutcome.taken);
- expect(p.state, SwarmPostState.confirmed);
- expect(r.taken, [p]);
+ 'and the post is told taken once', () {
+ // On a fake clock, a timeout passes only when the test says so, never
+ // because the machine is slow.
+ fakeAsync((async) {
+ final r = rig();
+ final p = post('op-1');
+ SwarmPostOutcome? first, again;
+ unawaited(r.box.add(p).then((outcome) => first = outcome));
+ async.elapse(SwarmOutbox.defaultTimeout);
+ expect(first, SwarmPostOutcome.lost);
+ unawaited(r.box.deliver(p).then((outcome) => again = outcome));
+ expect(p.state, SwarmPostState.sending);
+
+ r.answers[0].completeError(StateError('reset'));
+ async.elapse(
+ SwarmOutbox.defaultTimeout - const Duration(milliseconds: 1),
+ );
+ expect(p.state, SwarmPostState.sending, reason: 'the retry speaks now');
+
+ r.answers[1].complete({'result': <String, dynamic>{}});
+ async.flushMicrotasks();
+ expect(again, SwarmPostOutcome.taken);
+ expect(p.state, SwarmPostState.confirmed);
+ expect(r.taken, [p]);
+ });
});
test('read back before its answer, it leaves, and its answer still tells '
diff --git a/mobile/test/swarm/swarm_thread_test.dart b/mobile/test/swarm/swarm_thread_test.dart
index 9ff8f0a70..0da444ec8 100644
--- a/mobile/test/swarm/swarm_thread_test.dart
+++ b/mobile/test/swarm/swarm_thread_test.dart
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:typed_data';
+import 'package:fake_async/fake_async.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness_mobile/swarm/model.dart';
import 'package:harness_mobile/swarm/swarm_conversation.dart';
@@ -1427,75 +1428,89 @@ void main() {
await settle();
});
- test('no answer in time says Not sent; Retry reuses the receipt and the '
- 'late answer counts', () async {
- final r = await rig();
- final answers = held(r.swarm);
- final chat = SwarmConversation(
- r.reader,
- 'g1',
- sendTimeout: const Duration(milliseconds: 20),
+ /// A chat and its thread on [async]'s clock, the reply [text] in its box: a
+ /// timeout there passes only when the test says so, never because the
+ /// machine is slow.
+ ({FakeSwarm swarm, Map<String, Completer<Object?>> answers, SwarmThread t})
+ onFakeClock(FakeAsync async, String text) {
+ late final FakeSwarm swarm;
+ late final SwarmReader reader;
+ unawaited(
+ rig().then((r) {
+ swarm = r.swarm;
+ reader = r.reader;
+ }),
);
+ async.flushMicrotasks();
+ final answers = held(swarm);
+ final chat = SwarmConversation(reader, 'g1');
addTearDown(chat.dispose);
- await chat.load();
- r.swarm.routes[thread] = (_) => {
+ unawaited(chat.load());
+ async.flushMicrotasks();
+ swarm.routes[thread] = (_) => {
'messages': [message('m1', 1)],
'next': null,
};
final t = open(chat);
- await t.load();
- t.draft = 'Once only';
-
- expect(await t.send(), isFalse);
- final post = t.outgoing.single;
- expect(post.state, SwarmPostState.unconfirmed);
- expect(t.error, contains('not sent'));
+ unawaited(t.load());
+ async.flushMicrotasks();
+ t.draft = text;
+ return (swarm: swarm, answers: answers, t: t);
+ }
- final again = t.retry(post);
- await settle();
- final posts = [
- for (final call in r.swarm.calls)
- if (call.path == commands) call.body!['operationId'],
- ];
- expect(posts, hasLength(2));
- expect(posts.toSet(), hasLength(1), reason: 'one reply, one receipt');
- answers.values.single.complete({'result': <String, dynamic>{}});
- expect(await again, isTrue);
- expect(post.state, SwarmPostState.confirmed);
- expect(t.error, isNull);
+ test('no answer in time says Not sent; Retry reuses the receipt and the '
+ 'late answer counts', () {
+ fakeAsync((async) {
+ final (:swarm, :answers, :t) = onFakeClock(async, 'Once only');
+
+ bool? sent;
+ unawaited(t.send().then((value) => sent = value));
+ async.elapse(SwarmOutbox.defaultTimeout);
+ expect(sent, isFalse);
+ final post = t.outgoing.single;
+ expect(post.state, SwarmPostState.unconfirmed);
+ expect(t.error, contains('not sent'));
+
+ bool? again;
+ unawaited(t.retry(post).then((value) => again = value));
+ async.flushMicrotasks();
+ final posts = [
+ for (final call in swarm.calls)
+ if (call.path == commands) call.body!['operationId'],
+ ];
+ expect(posts, hasLength(2));
+ expect(posts.toSet(), hasLength(1), reason: 'one reply, one receipt');
+ // The last moment of the retry's own wait, whatever the machine's speed.
+ async.elapse(
+ SwarmOutbox.defaultTimeout - const Duration(milliseconds: 1),
+ );
+ expect(again, isNull);
+ answers.values.single.complete({'result': <String, dynamic>{}});
+ async.flushMicrotasks();
+ expect(again, isTrue);
+ expect(post.state, SwarmPostState.confirmed);
+ expect(t.error, isNull);
+ });
});
- test(
- 'a refusal that lands after Not sent still puts the reply back',
- () async {
- final r = await rig();
- final answers = held(r.swarm);
- final chat = SwarmConversation(
- r.reader,
- 'g1',
- sendTimeout: const Duration(milliseconds: 20),
- );
- addTearDown(chat.dispose);
- await chat.load();
- r.swarm.routes[thread] = (_) => {
- 'messages': [message('m1', 1)],
- 'next': null,
- };
- final t = open(chat);
- await t.load();
- t.draft = 'Too long';
+ test('a refusal that lands after Not sent still puts the reply back', () {
+ fakeAsync((async) {
+ final (swarm: _, :answers, :t) = onFakeClock(async, 'Too long');
- expect(await t.send(), isFalse);
+ bool? sent;
+ unawaited(t.send().then((value) => sent = value));
+ async.elapse(SwarmOutbox.defaultTimeout);
+ expect(sent, isFalse);
expect(t.draft, isEmpty);
answers.values.single.completeError(
const SuperTabFailure('TOO_LONG', 'Too long.', statusCode: 400),
);
- await settle();
+ async.flushMicrotasks();
expect(t.outgoing, isEmpty);
expect(t.draft, 'Too long');
expect(t.error, 'Too long.');
- },
- );
+ });
+ });
test('a refused reply goes back to the box while another stays on its '
'way', () async {