peppa/apps/web/app/(app)/runners/page.tsx
Асель 6b75324971 PEP-10/27/53: runners with a story queue, a git worktree per runner, free runners
- Runner settings with inheritance system → project → story (settings rows):
  mode story (default, one runner per story) / queue, context fresh / continue,
  workspace shared / worktree, maxRunners. No rows = behaviour as before.
- runner_tasks: a runner's story history and queue; sessions.name, sessions.workspace_path
  (migration 0007_runners).
- moveTask → startTaskOnRunner; runnerTick in the watchdog moves free queue runners to
  their next story (fresh: same tmux, new generation without resume; continue: message),
  reassigns queues of dead runners, removes finished runners' worktrees (clean + pushed).
- Free runners («Новый терминал»), rename, «Оформить стори»; the Stop hook doesn't ping
  for free runners or stories that left work.
- Web: «Новый терминал» (runners page, project sessions tab) with voice input,
  project/story runner settings, runner stories in the details panel; CLI peppa runner …
- e2e: packages/testkit/test/runners.test.ts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 06:38:02 +05:00

115 lines
6.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Link from "next/link";
import { Search, SquareTerminal } from "lucide-react";
import { listUserProjects, runnerStories, listUserSessions, RUNNER_FILTERS, sessionSteps, type RunnerFilter, type SessionView } from "@peppa/core";
import { LiveRefresh } from "@/components/LiveRefresh";
import { Empty, Flash, PageHeader } from "@/components/common";
import { RunnerDetails, RunnerRow } from "@/components/RunnerConsole";
import { Input } from "@/components/ui/input";
import { NewRunnerForm } from "@/components/Runners";
import { SettingsDialog } from "@/components/SettingsDialog";
import { Workspace } from "@/components/Workspace";
import { requireUser } from "@/lib/auth";
import { db } from "@/lib/db";
import { inbox, sessionEvents } from "@/lib/queries";
import { cn } from "@/lib/utils";
const FILTERS: Array<{ key: RunnerFilter | undefined; label: string }> = [
{ key: undefined, label: "все" },
{ key: "waiting", label: "ждут тебя" },
{ key: "working", label: "работают" },
{ key: "stopped", label: "упали" },
{ key: "finished", label: "готово" },
];
const inFilter = (s: SessionView, f: RunnerFilter) => (RUNNER_FILTERS[f] as string[]).includes(s.status);
const matches = (s: SessionView, q: string) =>
[s.name, s.taskKey, s.taskTitle, s.projectKey, s.tmuxName, `#${s.id}`].some((v) => v?.toLowerCase().includes(q));
type Params = { error?: string; f?: string; q?: string; id?: string };
export default async function Runners({ searchParams }: { searchParams: Promise<Params> }) {
const user = await requireUser();
const { error, f, q: rawQ, id } = await searchParams;
const filter = f && f in RUNNER_FILTERS ? (f as RunnerFilter) : undefined;
const q = rawQ?.trim().toLowerCase() ?? "";
const [all, open, projects] = await Promise.all([listUserSessions(db, user.id), inbox(user.id), listUserProjects(db, user.id)]);
const asking = new Map<number, typeof open>();
for (const x of open) if (x.sessionId) asking.set(x.sessionId, [...(asking.get(x.sessionId) ?? []), x]);
const needsPm = (s: SessionView) => s.status === "waiting_input" || asking.has(s.id);
const found = q ? all.filter((s) => matches(s, q)) : all;
const count = (k: RunnerFilter | undefined) => (k ? found.filter((s) => inFilter(s, k)).length : found.length);
// Who needs the PM first, then live runners, then finished ones; newest first inside each group.
const rank = (s: SessionView) => (needsPm(s) ? 0 : ["done", "failed"].includes(s.status) ? 2 : 1);
const shown = (filter ? found.filter((s) => inFilter(s, filter)) : found).sort((a, b) => rank(a) - rank(b) || b.id - a.id);
const url = (p: { f?: string; q?: string; id?: number | string }) => {
const sp = new URLSearchParams();
if (p.f) sp.set("f", p.f);
if (p.q) sp.set("q", p.q);
if (p.id) sp.set("id", String(p.id));
const s = sp.toString();
return s ? `/runners?${s}` : "/runners";
};
const base = { f: filter, q: rawQ?.trim() || undefined };
const selected = (id && all.find((s) => s.id === Number(id))) || shown[0];
const back = url({ ...base, id: selected?.id });
const aside = selected ? (
<RunnerDetails s={selected} questions={asking.get(selected.id) ?? []} events={await sessionEvents(selected.id)} steps={await sessionSteps(db, selected.id)} stories={await runnerStories(db, selected.id)} back={back} closeHref={url(base)} chosen={!!id} />
) : null;
return (
<Workspace aside={aside} asideWideOnly={!id}>
<LiveRefresh scope="runners" />
<Flash error={error} />
<PageHeader
title="Раннеры"
actions={
projects.length > 0 && (
<SettingsDialog title="Новый терминал" trigger={<><SquareTerminal />Новый терминал</>}>
<NewRunnerForm projects={projects} back="/runners" />
</SettingsDialog>
)
}
>
Агенты в tmux по стори твоих и общих проектов и свободные терминалы: кто ждёт тебя, кто работает, что готово.
</PageHeader>
<div className="mb-3 flex flex-col gap-3 lg:flex-row lg:items-center">
<nav className="flex flex-wrap gap-1.5" aria-label="Фильтр раннеров">
{FILTERS.map((x) => {
const n = count(x.key);
const active = x.key === filter;
return (
<Link
key={x.label}
href={url({ ...base, f: x.key })}
aria-current={active ? "page" : undefined}
className={cn(
"flex items-center gap-1.5 rounded-md border bg-card px-2.5 py-1 text-sm text-muted-foreground hover:border-muted-foreground hover:text-foreground hover:no-underline",
active && "border-primary/60 bg-primary/10 text-foreground",
)}
>
<span className={cn("font-mono font-semibold tabular-nums", x.key === "waiting" && n > 0 ? "text-primary" : "text-foreground")}>{n}</span>
{x.label}
</Link>
);
})}
</nav>
<form action="/runners" className="relative lg:ml-auto lg:w-64" role="search">
{filter && <input type="hidden" name="f" value={filter} />}
<Search className="pointer-events-none absolute top-1/2 left-2.5 size-4 -translate-y-1/2 text-muted-foreground" />
<Input type="search" name="q" data-hotkey-search defaultValue={rawQ ?? ""} placeholder="Стори, проект, tmux…" aria-label="Поиск раннеров" className="h-8 pl-8" />
</form>
</div>
{shown.length === 0 ? (
<Empty>{q || filter ? "Таких раннеров нет." : "Раннеров пока не было: перенеси стори в «В работе» на доске проекта."}</Empty>
) : (
<ul className="m-0 flex list-none flex-col gap-1 rounded-lg border bg-card/50 p-1.5">
{shown.map((s) => (
<RunnerRow key={s.id} s={s} href={url({ ...base, id: s.id })} selected={s.id === selected?.id && (id ? true : "wide")} needsPm={needsPm(s)} />
))}
</ul>
)}
</Workspace>
);
}