mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-17 04:21:24 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Each agent runs at most `runtimeConfig.heartbeat.maxConcurrentRuns` tasks at once. Other runs wait in the agent's queue with status `queued` > - On hosted staging, one agent had all 20 slots full and 295 queued runs. Some tasks waited 3 to 7 hours and never started > - The UI showed these tasks as live or working: a pulsing "Live" badge in the task header and in task rows, a spinning "Working … for 4 hours" in the task chat, and "N live" counts that included every queued run > - People could not tell that work was waiting, why it waited, or what to change > - This pull request makes a deep queue an honest, visible state: "Waiting for Codie · 20 slots max · 41 of 295 in line · Configure" > - The benefit is that operators see the real state and the one setting that changes it ## Linked Issues or Issue Description Refs #21391, #21349, #21242 **What happened?** A queued run that never started looked the same as a running run. The task header, the task rows in lists and the inbox, the task chat and the sidebar counts all showed queued runs as live or working. **Expected behavior** A run that waits for one of its agent's concurrency slots shows that it waits. It names the agent, the agent's limit and the run's place in line, and it links to where the limit is set. The place in line is the order in which the server starts the agent's queued runs. **Why does it matter?** With a deep queue, the UI said "working" for hours while nothing ran. Operators could not see the cause or the fix. ## What Changed - Server: a new module `server/src/services/heartbeat/queued-run-line.ts` now holds the facts that the queued-run dispatcher orders by (`queuedRunDispatchFacts`). The dispatch path in `queue.ts` and the new "place in line" read use this one builder, so the line the UI shows is the dispatch order. The logic moved without change, including the recovery-share fix from #21391. - Server: `executionProjectionsForRuns` adds `agentQueue` (agent, limit, running count, position, queued count) and the label "Waiting for <agent>" with cause `agent_at_capacity` to a queued, never-started run when its agent has no free slot. It reads one batch of agents and running counts. Then it builds one line per full agent (at most 8 agents per read), using only the three context keys that ordering needs. No new polling. - Server: the place in line uses `queuedRunDispatchLine`, an O(n log n) replay of the dispatcher. The plain replay rescans the queue after each step, which is quadratic (measured: 3.6 ms at 300 runs, 76 ms at 3,000, 600 ms at 10,000, all on the event loop). Dispatch itself is unchanged: it stops after the free slots. Each agent's line is memoized for 5 s and shared by concurrent reads. In a preview worktree, the line applies the same creation-time cutoff as dispatch. - Server: the task list summary adds `runningDescendantCount` (descendants whose run started) next to `liveDescendantCount`. `liveDescendantCount` still decides whether a blocked parent is covered. The "n live below" chip uses the new count. - Shared: the new `ExecutionAgentQueue` type and the optional field `ExecutionProjection.agentQueue`. - Server: `/companies/:companyId/live-runs` lists running runs before queued runs, so a deep queue cannot push running work out of the 50-row limit. - UI: a new `AgentQueueWait` component shows "Waiting for Codie · 20 slots max · 41 of 295 in line · Configure", with no spinner and no elapsed timer. Configure opens the agent's Advanced view, where the run policy (`maxConcurrentRuns`) is edited. The task chat (both renderers), the run ledger and the Storybook story use it. - UI: the task header and the task rows (task list, inbox, legacy views) show a quiet "Waiting" instead of the pulsing "Live" when every live run of the task has not started. The full sentence shows on hover. "n live below" counts only started sub-tasks. A waiting sub-task still covers its blocked parent. - UI: the sidebar "N live" counts (Dashboard, agents) count only started runs. - UI: the task chat pill now names other server holds on a queued run (for example "Waiting for maintenance") instead of "Working". - Docs: DESIGN.md records the waiting-for-slot hold. - Storybook: `runs-agent-queue-wait` stories (full line, task chat, task row, one slot, mobile), with play checks. ## Verification - Red/green server test: `server/src/services/execution-projection-agent-queue.test.ts` (embedded Postgres). Before this change, the projection has no `agentQueue`. Now it checks the agent name, limit, running count, positions 1 to 4 in dispatch order (a person's retry first, then priority, not creation order), no wait while a slot is free, and isolation from other companies' runs. It also checks that the positions equal the line that the dispatch builder produces. - `pnpm exec vitest run` on the new test, `queued-run-dispatch-order`, `recovery-wake-herd`, `heartbeat-queued-run-claim-isolation` and `agent-live-run-routes` in `server/`. - Runtime cost (rule `runtime-dependency-boundaries`): the assumption is that the line stays cheap for a deep queue. `queued-run-line.test.ts` checks that `queuedRunDispatchLine` equals the dispatcher's claim-every-run order over 400 random queues (ages around the aging boundary, recovery and person runs, blocked runs, deferred share runs, all share counters). It also orders 10,000 runs in under 250 ms (the quadratic replay takes about 600 ms). The DB test projects a 300-run queue end to end with correct positions 1 to 300. Remaining limit: the line is up to 5 s stale. - Red/green: `issue-list-assignee-filter-routes.test.ts` now asserts `runningDescendantCount: 0` for a parent whose only live descendant is queued and never started. - UI tests: `liveIssueIds.test.ts` (waiting tasks, started runs keep "Live", no "running below" count for waiting sub-tasks) and `TaskChatLiveRunPill.test.tsx` (waiting line with no spinner and the Configure link, other holds named). The suites for the run ledger, sidebar, sidebar agents, task detail, task columns and task chat also pass. - The Storybook stories render in headless Chromium and their play checks pass. - After deploy: open a queued task on staging and confirm the waiting state. ## Risks - Cost on read: a projection read that includes a queued run of a full agent now reads that agent's queued runs (only small columns) and the dispatch facts. This is at most one line per full agent every 5 s, for at most 8 agents per read. Reads with no queued, never-started runs do no more work. - The place in line is the server's prediction of dispatch order. A claim that is refused at start time (a gate, a blocker) can let the next run start first. The position updates on the next refresh. - The process-local "declined aged claim" hint that the dispatcher uses is not visible to other servers, so the projection does not use it. This can change only the aged-oldest share step. - Configure opens the agent's Advanced view (the YAML run policy). The agent's Runtime tab hides the run-policy fields today. - Low risk otherwise: no schema change and no migration. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use (shell, file edits, headless browser) and subagents for the UI survey. ## 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 - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [ ] 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>
143 lines
5.6 KiB
TypeScript
143 lines
5.6 KiB
TypeScript
import type { ExecutionAgentQueue, IssueStatus } from "@paperclipai/shared";
|
|
import { agentQueueWaitOf, isWaitingToStart } from "./agent-queue-wait";
|
|
import type { LiveRunForIssue } from "../api/heartbeats";
|
|
|
|
function isLiveRunStatus(status: string): boolean {
|
|
return status === "queued" || status === "running";
|
|
}
|
|
|
|
const TERMINAL_ISSUE_STATUSES = new Set<IssueStatus>(["done", "cancelled"]);
|
|
|
|
export function isTerminalIssueStatus(status: string | null | undefined): status is IssueStatus {
|
|
return TERMINAL_ISSUE_STATUSES.has(status as IssueStatus);
|
|
}
|
|
|
|
export function isLiveIssueRun(
|
|
run: Pick<LiveRunForIssue, "status">,
|
|
issueStatus?: string | null,
|
|
): boolean {
|
|
return isLiveRunStatus(run.status) && !isTerminalIssueStatus(issueStatus);
|
|
}
|
|
|
|
export interface LiveIssueStatusNode {
|
|
id: string;
|
|
status: IssueStatus | string;
|
|
updatedAt?: Date | string | number | null;
|
|
}
|
|
|
|
function collectIssueStatusById(issues: readonly LiveIssueStatusNode[] | null | undefined): Map<string, string> {
|
|
const snapshotByIssueId = new Map<string, { status: string; updatedAtMs: number | null }>();
|
|
for (const issue of issues ?? []) {
|
|
const candidate = {
|
|
status: issue.status,
|
|
updatedAtMs: issueUpdatedAtMs(issue.updatedAt),
|
|
};
|
|
const existing = snapshotByIssueId.get(issue.id);
|
|
if (!existing || shouldReplaceIssueStatusSnapshot(existing, candidate)) snapshotByIssueId.set(issue.id, candidate);
|
|
}
|
|
return new Map([...snapshotByIssueId].map(([issueId, snapshot]) => [issueId, snapshot.status]));
|
|
}
|
|
|
|
function issueUpdatedAtMs(updatedAt: LiveIssueStatusNode["updatedAt"]): number | null {
|
|
if (updatedAt === null || updatedAt === undefined) return null;
|
|
const timestamp = updatedAt instanceof Date ? updatedAt.getTime() : new Date(updatedAt).getTime();
|
|
return Number.isFinite(timestamp) ? timestamp : null;
|
|
}
|
|
|
|
function shouldReplaceIssueStatusSnapshot(
|
|
existing: { status: string; updatedAtMs: number | null },
|
|
candidate: { status: string; updatedAtMs: number | null },
|
|
): boolean {
|
|
if (candidate.updatedAtMs !== null && existing.updatedAtMs !== null) {
|
|
if (candidate.updatedAtMs !== existing.updatedAtMs) return candidate.updatedAtMs > existing.updatedAtMs;
|
|
} else if (candidate.updatedAtMs !== null) {
|
|
return true;
|
|
} else if (existing.updatedAtMs !== null) {
|
|
return false;
|
|
}
|
|
|
|
return !isTerminalIssueStatus(existing.status) && isTerminalIssueStatus(candidate.status);
|
|
}
|
|
|
|
/** A task whose live runs have all not started yet; `queue` names the agent's line when the server reports it. */
|
|
export interface IssueWaiting {
|
|
queue: ExecutionAgentQueue | null;
|
|
}
|
|
|
|
const waitingByIssueIds = new WeakMap<ReadonlySet<string>, ReadonlyMap<string, IssueWaiting>>();
|
|
|
|
export function collectLiveIssueIds(
|
|
liveRuns: readonly LiveRunForIssue[] | null | undefined,
|
|
issues?: readonly LiveIssueStatusNode[] | null,
|
|
): Set<string> {
|
|
const ids = new Set<string>();
|
|
const started = new Set<string>();
|
|
const waiting = new Map<string, IssueWaiting>();
|
|
const statusByIssueId = collectIssueStatusById(issues);
|
|
for (const run of liveRuns ?? []) {
|
|
if (!run.issueId || !isLiveIssueRun(run, statusByIssueId.get(run.issueId))) continue;
|
|
ids.add(run.issueId);
|
|
if (!isWaitingToStart(run)) started.add(run.issueId);
|
|
else if (!waiting.get(run.issueId)?.queue) waiting.set(run.issueId, { queue: agentQueueWaitOf(run.execution) });
|
|
}
|
|
for (const issueId of started) waiting.delete(issueId);
|
|
waitingByIssueIds.set(ids, waiting);
|
|
return ids;
|
|
}
|
|
|
|
/** Whether a live task from {@link collectLiveIssueIds} is only waiting to start. */
|
|
export function issueWaitingOf(
|
|
liveIssueIds: ReadonlySet<string> | null | undefined,
|
|
issueId: string,
|
|
): IssueWaiting | null {
|
|
return liveIssueIds ? waitingByIssueIds.get(liveIssueIds)?.get(issueId) ?? null : null;
|
|
}
|
|
|
|
/**
|
|
* Minimal tree node shape needed to roll live descendants up to their ancestors.
|
|
* Both list and inbox issue objects satisfy this.
|
|
*/
|
|
export interface SubtreeLiveNode {
|
|
id: string;
|
|
parentId: string | null;
|
|
}
|
|
|
|
/**
|
|
* Derive, for every issue in the already-loaded tree, how many of its
|
|
* descendants currently have their own live (queued/running) run.
|
|
*
|
|
* The count is strictly over descendants — an issue's own live run never
|
|
* contributes to its own entry. Ancestors are walked through the loaded set
|
|
* via `parentId`, so descendants that are not loaded are simply not counted.
|
|
*
|
|
* Pair with {@link collectLiveIssueIds}: keep `Live` for `liveIssueIds.has(id)`
|
|
* (own run) and render the distinct "n live below" treatment only when an
|
|
* issue is not itself live but has a positive subtree-live count.
|
|
*/
|
|
export function collectSubtreeLiveCounts(
|
|
issues: readonly SubtreeLiveNode[] | null | undefined,
|
|
liveIssueIds: ReadonlySet<string>,
|
|
options: { startedOnly?: boolean } = {},
|
|
): Map<string, number> {
|
|
const counts = new Map<string, number>();
|
|
if (!issues || issues.length === 0 || liveIssueIds.size === 0) return counts;
|
|
|
|
const parentById = new Map<string, string | null>();
|
|
for (const issue of issues) parentById.set(issue.id, issue.parentId);
|
|
|
|
for (const liveId of liveIssueIds) {
|
|
// Only roll up live issues that belong to the loaded tree. With
|
|
// `startedOnly`, a sub-task that only waits to start is not running below;
|
|
// it still counts as live work that covers a blocked parent.
|
|
if (!parentById.has(liveId) || (options.startedOnly && issueWaitingOf(liveIssueIds, liveId))) continue;
|
|
const seen = new Set<string>([liveId]);
|
|
let parentId = parentById.get(liveId) ?? null;
|
|
while (parentId && !seen.has(parentId)) {
|
|
seen.add(parentId);
|
|
counts.set(parentId, (counts.get(parentId) ?? 0) + 1);
|
|
parentId = parentById.get(parentId) ?? null;
|
|
}
|
|
}
|
|
return counts;
|
|
}
|