mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-20 18:26:40 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - The heartbeat queue decides which queued run an agent starts next.
It puts system recovery in the lowest tier.
> - When a run hits the tool-call loop limit, the server queues a
continuation with the wake reason `issue_continuation_needed`. The
source run succeeded. It did not fail.
> - The queue classed that continuation as system recovery. While any
person-requested run was queued, it got no share. It started only when
the 60-minute aging bound expired.
> - In a restart-chaos wave, 4 of 7 loop continuations waited 60:00 to
60:28. One task broke the "no rest within budget" rule. One run then
resumed an hour-old sandbox, timed out and showed a Retry banner.
> - This pull request schedules a clean loop-limit continuation in the
tier of its task, so a person's task that continues its own work stays
with that person's work.
> - Real failure recovery keeps the recovery tier and the aggregate
share, so a failure loop cannot become high-priority churn.
> - The benefit is that a person's task continues within seconds, not
one hour, and the stale-sandbox Retry path is not reached.
## Linked Issues or Issue Description
Refs #21349
**What happened?**
A continuation queued after the tool-call loop limit waited for the
60-minute aging bound (`QUEUED_RUN_AGING_MS`) on an agent that had
person-requested runs queued. 4 of 7 loop continuations in one
restart-chaos full wave waited 60:00 to 60:28.
**Expected behavior**
A continuation of a person's task whose source run succeeded starts in
the same tier as that person's work. Failure retries stay in the
recovery tier.
**Steps to reproduce**
1. Queue a person's task on an agent with a concurrency limit of 1.
2. Let the run hit the tool-call loop limit, so the server queues an
`issue_continuation_needed` continuation with
`livenessContinuationReason: agent_tool_call_loop`.
3. Keep person-requested runs queued on the same agent.
4. The continuation waits until it is 60 minutes old.
**Paperclip version or commit**
`master` at 70a4351343.
## What Changed
- `queued-run-dispatch-order.ts`: add a `cleanContinuation` dispatch
fact. A clean continuation is not system recovery, so it takes the tier
of its task (person or work) and the normal readiness, priority and age
order. The module stays domain-agnostic: the wake reason is defined once
(`ISSUE_CONTINUATION_NEEDED_WAKE_REASON`, used in
`RECOVERY_WAKE_REASONS`).
- `queued-run-dispatch-order.ts`: one core predicate
(`countsAsRecoveryStart`) drives `isSystemRecoveryRun`, the in-pass
recovery counter, the place-in-line counter and the start-history
counter. Every start with a recovery wake reason that a person did not
ask for still uses up the recovery share. A clean continuation therefore
does not add recovery-reason starts.
- New `queued-run-provenance.ts`: the clean-continuation causes, the
source-run check and `QUEUED_RUN_ORDER_CONTEXT_KEYS`.
- `queue.ts` (`listQueuedRunCandidates`, the real dispatch query) and
`queued-run-line.ts` (the place-in-line query) both read
`QUEUED_RUN_ORDER_CONTEXT_KEYS`, so the dispatcher sees `retryReason`,
`livenessContinuationReason` and `livenessContinuationSourceRunId`.
- `queued-run-line.ts`: set `cleanContinuation` only when all of these
are true:
- the wake reason is `issue_continuation_needed` and
`livenessContinuationReason` is `agent_tool_call_loop`;
- the run has no `retryOfRunId`, `retryReason` or `previousRunId` (it is
not a failure retry);
- the source run (`livenessContinuationSourceRunId`, same company) has
status `succeeded`;
- the source run is not itself a loop continuation, so only the first
hop of a loop chain gets the faster tier.
## Verification
- `pnpm --filter @paperclipai/server exec vitest run
src/services/heartbeat/queued-run-dispatch-order.test.ts
src/services/heartbeat/queued-run-line.test.ts
src/services/execution-projection-agent-queue.test.ts` passes (39
tests), plus `src/services/heartbeat/`,
`queued-run-dispatch-stacking.test.ts` and `recovery-wake-herd.test.ts`.
- Red before the fix, green after it:
- `starts a person's task continuing after the tool-call loop limit with
that person's work, not after the aging bound` (unit; failed with
`expected 'recovery' to be 'person'`)
- `counts a clean continuation's start against system recovery's share,
as the start history does` (unit)
- `starts a person's task continuing after the tool-call loop limit with
that person's work, and keeps failure recovery behind it` (embedded
Postgres; a failure retry, a chained loop continuation and a
continuation of a failed run all stay behind the person's work)
- Assembled heartbeat service
(`src/__tests__/heartbeat-clean-continuation-dispatch.test.ts`, embedded
Postgres, `resumeQueuedRunsForAgent` with one free slot), checks which
queued run actually starts:
- `starts a clean loop-limit continuation of a person's task before a
newer person assignment` (red without the `queue.ts` change, green with
it)
- `starts a newer person assignment before a failure retry of a person's
task` (guard)
- Guard that passes before and after: `keeps a failure retry of a
person's task in the recovery tier while people wait`.
- The `queuedRunDispatchLine` fuzz test now also generates clean
continuations and still matches the dispatcher order for 400 seeds.
- `pnpm verify` ran locally.
## Risks
- Low risk. The change is limited to dispatch order. No schema or API
change.
- A clean continuation can now start ahead of agent work. It is bounded:
one continuation per source run (existing idempotency key), only the
first hop of a loop chain, and only after a succeeded source run. A loop
chain's later hops and all failure retries stay in the recovery tier
with the existing share.
- Recovery-reason starts per hour do not increase. A clean continuation
start uses up recovery's share like before.
> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude
Code as an agent with tool use (shell, file edit, test runs). Extended
reasoning on.
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
246 lines
16 KiB
TypeScript
246 lines
16 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { eq } from "drizzle-orm";
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
import { agentWakeupRequests, agents, companies, createDb, heartbeatRuns, issues } from "@paperclipai/db";
|
|
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../__tests__/helpers/embedded-postgres.js";
|
|
import { executionProjectionsForRuns } from "./execution-projection.js";
|
|
import { agentQueuedRunLine, clearAgentQueueLineCache, queuedRunDispatchFacts, worktreeExecutionCutoff } from "./heartbeat/queued-run-line.js";
|
|
import { queuedRunDispatchClass } from "./heartbeat/queued-run-dispatch-order.js";
|
|
import { issueService } from "./issues.js";
|
|
|
|
const support = await getEmbeddedPostgresTestSupport();
|
|
const now = new Date("2026-11-13T12:00:00Z");
|
|
const minutesAgo = (minutes: number) => new Date(now.getTime() - minutes * 60_000);
|
|
|
|
(support.supported ? describe : describe.skip)("execution projection: waiting behind the agent's limit", () => {
|
|
let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
|
|
let db: ReturnType<typeof createDb>;
|
|
beforeAll(async () => {
|
|
database = await startEmbeddedPostgresTestDatabase("agent-queue-projection-");
|
|
db = createDb(database.connectionString);
|
|
}, 60_000);
|
|
afterAll(async () => { await database?.cleanup(); });
|
|
beforeEach(() => clearAgentQueueLineCache());
|
|
|
|
async function fixture(options: { maxConcurrentRuns: number; running: number }) {
|
|
const companyId = randomUUID(), agentId = randomUUID();
|
|
await db.insert(companies).values({ id: companyId, name: "Queue test", issuePrefix: `Q${companyId.slice(0, 8)}` });
|
|
await db.insert(agents).values({ id: agentId, companyId, name: "Codie", adapterType: "claude_local",
|
|
runtimeConfig: { heartbeat: { maxConcurrentRuns: options.maxConcurrentRuns } } });
|
|
for (let index = 0; index < options.running; index += 1) {
|
|
await db.insert(heartbeatRuns).values({ companyId, agentId, status: "running",
|
|
startedAt: minutesAgo(90), createdAt: minutesAgo(90) });
|
|
}
|
|
async function queue(input: { priority: "critical" | "high" | "medium" | "low"; createdMinutesAgo: number; wakeReason?: string }) {
|
|
const issueId = randomUUID(), runId = randomUUID();
|
|
await db.insert(issues).values({ id: issueId, companyId, title: `Task ${input.priority}`, assigneeAgentId: agentId,
|
|
status: "in_progress", priority: input.priority });
|
|
await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, status: "queued", issueId,
|
|
createdAt: minutesAgo(input.createdMinutesAgo),
|
|
contextSnapshot: { issueId, ...(input.wakeReason ? { wakeReason: input.wakeReason } : {}) } });
|
|
return runId;
|
|
}
|
|
return { companyId, agentId, queue };
|
|
}
|
|
|
|
it("names the agent, its limit and each run's place in the order dispatch starts them", async () => {
|
|
const f = await fixture({ maxConcurrentRuns: 2, running: 2 });
|
|
// Created oldest first, but dispatch starts a person's retry first, then by priority.
|
|
const lowOld = await f.queue({ priority: "low", createdMinutesAgo: 30 });
|
|
const medium = await f.queue({ priority: "medium", createdMinutesAgo: 20 });
|
|
const critical = await f.queue({ priority: "critical", createdMinutesAgo: 10 });
|
|
const personRetry = await f.queue({ priority: "low", createdMinutesAgo: 5, wakeReason: "task_retry" });
|
|
|
|
const projections = await executionProjectionsForRuns(db, f.companyId, [lowOld, medium, critical, personRetry], now);
|
|
const queueOf = (runId: string) => projections.get(runId)?.agentQueue;
|
|
expect(projections.get(critical)).toMatchObject({
|
|
phase: "queued",
|
|
label: "Waiting for Codie",
|
|
cause: "agent_at_capacity",
|
|
agentQueue: { agentId: f.agentId, agentName: "Codie", maxConcurrentRuns: 2, runningCount: 2, position: 2, queuedCount: 4 },
|
|
});
|
|
expect([personRetry, critical, medium, lowOld].map(runId => queueOf(runId)?.position)).toEqual([1, 2, 3, 4]);
|
|
|
|
// The position is the dispatch line itself, not a client-side sort.
|
|
const [agent] = await db.select().from(agents).where(eq(agents.id, f.agentId));
|
|
expect(await agentQueuedRunLine(db, issueService(db), agent!, now)).toEqual([personRetry, critical, medium, lowOld]);
|
|
});
|
|
|
|
it("does not claim a run waits on the limit while the agent has a free slot", async () => {
|
|
const f = await fixture({ maxConcurrentRuns: 3, running: 2 });
|
|
const runId = await f.queue({ priority: "high", createdMinutesAgo: 5 });
|
|
const projection = (await executionProjectionsForRuns(db, f.companyId, [runId], now)).get(runId);
|
|
expect(projection).toMatchObject({ phase: "queued", label: "Queued", cause: null });
|
|
expect(projection?.agentQueue).toBeUndefined();
|
|
|
|
// Once the limit is full the same run explains the wait.
|
|
await db.update(agents).set({ runtimeConfig: { heartbeat: { maxConcurrentRuns: 2 } } }).where(eq(agents.id, f.agentId));
|
|
expect((await executionProjectionsForRuns(db, f.companyId, [runId], now)).get(runId)).toMatchObject({
|
|
label: "Waiting for Codie",
|
|
agentQueue: { maxConcurrentRuns: 2, runningCount: 2, position: 1, queuedCount: 1 },
|
|
});
|
|
});
|
|
|
|
it("names the routine's open execution that holds a queued routine task, before the agent's limit", async () => {
|
|
const f = await fixture({ maxConcurrentRuns: 20, running: 0 });
|
|
const routineId = randomUUID();
|
|
const holderIssueId = randomUUID(), holderRunId = randomUUID();
|
|
const waitingIssueId = randomUUID(), waitingRunId = randomUUID();
|
|
await db.insert(heartbeatRuns).values({ id: holderRunId, companyId: f.companyId, agentId: f.agentId, status: "running",
|
|
startedAt: minutesAgo(18), createdAt: minutesAgo(30), issueId: holderIssueId, contextSnapshot: { issueId: holderIssueId } });
|
|
await db.insert(issues).values([
|
|
{ id: holderIssueId, companyId: f.companyId, identifier: "Q-3093", title: "Nightly test gardening", status: "in_progress",
|
|
assigneeAgentId: f.agentId, originKind: "routine_execution", originId: routineId, executionRunId: holderRunId },
|
|
{ id: waitingIssueId, companyId: f.companyId, identifier: "Q-3636", title: "Nightly test gardening", status: "in_progress",
|
|
assigneeAgentId: f.agentId, originKind: "routine_execution", originId: routineId },
|
|
]);
|
|
await db.insert(heartbeatRuns).values({ id: waitingRunId, companyId: f.companyId, agentId: f.agentId, status: "queued",
|
|
createdAt: minutesAgo(8), issueId: waitingIssueId, contextSnapshot: { issueId: waitingIssueId } });
|
|
|
|
expect((await executionProjectionsForRuns(db, f.companyId, [waitingRunId], now)).get(waitingRunId)).toMatchObject({
|
|
phase: "queued",
|
|
label: "Waiting for Q-3093 to finish",
|
|
cause: "routine_execution_in_progress",
|
|
});
|
|
// The holder's own run is not waiting on itself.
|
|
expect((await executionProjectionsForRuns(db, f.companyId, [holderRunId], now)).get(holderRunId)?.cause).not.toBe("routine_execution_in_progress");
|
|
|
|
// A private holder is not named to readers of the waiting task.
|
|
await db.update(issues).set({ visibility: "private" }).where(eq(issues.id, holderIssueId));
|
|
const privateHold = (await executionProjectionsForRuns(db, f.companyId, [waitingRunId], now)).get(waitingRunId);
|
|
expect(privateHold).toMatchObject({ label: "Waiting for another task in this routine to finish", cause: "routine_execution_in_progress" });
|
|
expect(JSON.stringify(privateHold)).not.toContain("Q-3093");
|
|
await db.update(issues).set({ visibility: "open" }).where(eq(issues.id, holderIssueId));
|
|
|
|
// Once the holder closes, the routine slot is free and the run is plainly queued.
|
|
await db.update(issues).set({ status: "done" }).where(eq(issues.id, holderIssueId));
|
|
expect((await executionProjectionsForRuns(db, f.companyId, [waitingRunId], now)).get(waitingRunId)).toMatchObject({
|
|
label: "Queued", cause: null,
|
|
});
|
|
});
|
|
|
|
it("leaves out queued runs that a preview worktree's cutoff keeps from starting", async () => {
|
|
const f = await fixture({ maxConcurrentRuns: 1, running: 1 });
|
|
const copiedOld = await f.queue({ priority: "critical", createdMinutesAgo: 120 });
|
|
const fresh = await f.queue({ priority: "low", createdMinutesAgo: 5 });
|
|
const [agent] = await db.select().from(agents).where(eq(agents.id, f.agentId));
|
|
expect(await agentQueuedRunLine(db, issueService(db), agent!, now, { cutoff: null })).toEqual([copiedOld, fresh]);
|
|
expect(await agentQueuedRunLine(db, issueService(db), agent!, now, { cutoff: minutesAgo(60) })).toEqual([fresh]);
|
|
// Outside a worktree there is no cutoff and no settings read.
|
|
expect(await worktreeExecutionCutoff(db, {})).toBeNull();
|
|
});
|
|
|
|
it("keeps a deep queue's place in line to a few reads, not one per run", async () => {
|
|
const f = await fixture({ maxConcurrentRuns: 20, running: 20 });
|
|
const runIds: string[] = [];
|
|
for (let index = 0; index < 300; index += 1) {
|
|
runIds.push(await f.queue({ priority: (["critical", "high", "medium", "low"] as const)[index % 4], createdMinutesAgo: 300 - index,
|
|
...(index % 9 === 0 ? { wakeReason: "issue_continuation_needed" } : {}) }));
|
|
}
|
|
const started = performance.now();
|
|
const projections = await executionProjectionsForRuns(db, f.companyId, runIds, now);
|
|
const elapsedMs = performance.now() - started;
|
|
const positions = runIds.map(runId => projections.get(runId)?.agentQueue?.position).sort((a, b) => a! - b!);
|
|
expect(positions).toEqual(runIds.map((_, index) => index + 1));
|
|
expect(projections.get(runIds[0]!)?.agentQueue?.queuedCount).toBe(300);
|
|
// Generous bound for slow CI; the ordering itself is O(n log n).
|
|
expect(elapsedMs).toBeLessThan(5_000);
|
|
});
|
|
|
|
it("starts work on a task tree a person created ahead of a newer person assignment, and leaves an agent's tree behind it", async () => {
|
|
const f = await fixture({ maxConcurrentRuns: 1, running: 1 });
|
|
const task = async (input: { createdByUserId?: string; parentId?: string }) => {
|
|
const issueId = randomUUID();
|
|
await db.insert(issues).values({ id: issueId, companyId: f.companyId, title: "Task", assigneeAgentId: f.agentId, status: "todo",
|
|
priority: "medium", parentId: input.parentId ?? null, createdByUserId: input.createdByUserId ?? null,
|
|
createdByAgentId: input.createdByUserId ? null : f.agentId });
|
|
return issueId;
|
|
};
|
|
const queueOn = async (issueId: string, wakeReason: string, createdMinutesAgo: number, byPerson = false) => {
|
|
const runId = randomUUID();
|
|
const wakeupRequestId = byPerson ? randomUUID() : null;
|
|
if (wakeupRequestId) await db.insert(agentWakeupRequests).values({ id: wakeupRequestId, companyId: f.companyId,
|
|
agentId: f.agentId, source: "assignment", reason: wakeReason, requestedByActorType: "user", requestedByActorId: "board-user" });
|
|
await db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: f.agentId, status: "queued", issueId,
|
|
wakeupRequestId, createdAt: minutesAgo(createdMinutesAgo), contextSnapshot: { issueId, wakeReason } });
|
|
return runId;
|
|
};
|
|
const personRoot = await task({ createdByUserId: "board-user" });
|
|
const child = await task({ parentId: personRoot });
|
|
const grandchild = await task({ parentId: child });
|
|
const agentRoot = await task({});
|
|
const agentChild = await task({ parentId: agentRoot });
|
|
const agentTreeWork = await queueOn(agentChild, "issue_assigned", 50);
|
|
const subtask = await queueOn(grandchild, "issue_assigned", 40);
|
|
const parentResume = await queueOn(personRoot, "issue_children_completed", 30);
|
|
const newAssignment = await queueOn(await task({ createdByUserId: "board-user" }), "issue_assigned", 5, true);
|
|
|
|
const [agent] = await db.select().from(agents).where(eq(agents.id, f.agentId));
|
|
expect(await agentQueuedRunLine(db, issueService(db), agent!, now)).toEqual([subtask, parentResume, newAssignment, agentTreeWork]);
|
|
});
|
|
|
|
it("starts a person's task continuing after the tool-call loop limit with that person's work, and keeps failure recovery behind it", async () => {
|
|
const f = await fixture({ maxConcurrentRuns: 1, running: 1 });
|
|
const personTask = async (status: "todo" | "in_progress") => {
|
|
const issueId = randomUUID();
|
|
await db.insert(issues).values({ id: issueId, companyId: f.companyId, title: "Task", assigneeAgentId: f.agentId, status,
|
|
priority: "medium", createdByUserId: "board-user" });
|
|
return issueId;
|
|
};
|
|
const sourceRun = async (issueId: string, status: "succeeded" | "failed", context: Record<string, unknown> = {}) => {
|
|
const runId = randomUUID();
|
|
await db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: f.agentId, status, issueId,
|
|
createdAt: minutesAgo(80), startedAt: minutesAgo(80), finishedAt: minutesAgo(70),
|
|
contextSnapshot: { issueId, wakeReason: "issue_assigned", ...context } });
|
|
return runId;
|
|
};
|
|
const queued = async (issueId: string, createdMinutesAgo: number, context: Record<string, unknown>,
|
|
input: { retryOfRunId?: string; byPerson?: boolean } = {}) => {
|
|
const runId = randomUUID();
|
|
const wakeupRequestId = input.byPerson ? randomUUID() : null;
|
|
if (wakeupRequestId) await db.insert(agentWakeupRequests).values({ id: wakeupRequestId, companyId: f.companyId,
|
|
agentId: f.agentId, source: "assignment", reason: "issue_assigned", requestedByActorType: "user", requestedByActorId: "board-user" });
|
|
await db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: f.agentId, status: "queued", issueId,
|
|
wakeupRequestId, retryOfRunId: input.retryOfRunId ?? null, createdAt: minutesAgo(createdMinutesAgo),
|
|
contextSnapshot: { issueId, ...context } });
|
|
return runId;
|
|
};
|
|
const loopContinuation = (sourceRunId: string) => ({ wakeReason: "issue_continuation_needed",
|
|
livenessContinuationReason: "agent_tool_call_loop", livenessContinuationSourceRunId: sourceRunId });
|
|
|
|
const loopTask = await personTask("in_progress");
|
|
const continuation = await queued(loopTask, 10, loopContinuation((await sourceRun(loopTask, "succeeded")).toUpperCase()));
|
|
const [alone] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, continuation));
|
|
const { facts: [aloneFacts] } = await queuedRunDispatchFacts(db, { agent: { id: f.agentId, companyId: f.companyId },
|
|
queuedRuns: [alone!], dependencyReadiness: new Map(), issueById: new Map([[loopTask, { status: "in_progress", priority: "medium",
|
|
createdByUserId: "board-user" }]]), now });
|
|
expect(aloneFacts!.cleanContinuation).toBe(true);
|
|
expect(queuedRunDispatchClass(aloneFacts!, now)).toBe("person");
|
|
const retryTask = await personTask("in_progress");
|
|
const failedRun = await sourceRun(retryTask, "failed");
|
|
const failureRetry = await queued(retryTask, 20, { ...loopContinuation(failedRun), retryReason: "issue_continuation_needed" },
|
|
{ retryOfRunId: failedRun });
|
|
const chainTask = await personTask("in_progress");
|
|
const chained = await queued(chainTask, 15, loopContinuation(await sourceRun(chainTask, "succeeded",
|
|
{ wakeReason: "issue_continuation_needed", livenessContinuationReason: "agent_tool_call_loop" })));
|
|
const failedSourceTask = await personTask("in_progress");
|
|
const afterFailure = await queued(failedSourceTask, 12, loopContinuation(await sourceRun(failedSourceTask, "failed")));
|
|
const people = [await queued(await personTask("todo"), 2, { wakeReason: "issue_assigned" }, { byPerson: true }),
|
|
await queued(await personTask("todo"), 1, { wakeReason: "issue_assigned" }, { byPerson: true })];
|
|
|
|
const [agent] = await db.select().from(agents).where(eq(agents.id, f.agentId));
|
|
const line = await agentQueuedRunLine(db, issueService(db), agent!, now);
|
|
expect(line.slice(0, 3)).toEqual([continuation, ...people]);
|
|
expect(line.slice(3).sort()).toEqual([failureRetry, chained, afterFailure].sort());
|
|
});
|
|
|
|
it("leaves running runs and other companies' runs out of the line", async () => {
|
|
const f = await fixture({ maxConcurrentRuns: 1, running: 1 });
|
|
const mine = await f.queue({ priority: "medium", createdMinutesAgo: 5 });
|
|
const other = await fixture({ maxConcurrentRuns: 1, running: 1 });
|
|
await other.queue({ priority: "critical", createdMinutesAgo: 50 });
|
|
const projection = (await executionProjectionsForRuns(db, f.companyId, [mine], now)).get(mine);
|
|
expect(projection?.agentQueue).toMatchObject({ position: 1, queuedCount: 1, runningCount: 1 });
|
|
});
|
|
});
|