peppa/apps/web/components/Runners.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

200 lines
9.9 KiB
TypeScript
Raw Permalink 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.

// PEP-10/27/53: «Новый терминал», runner settings (project / story) and a runner's stories.
import { Plus, SquareTerminal } from "lucide-react";
import {
RUNNER_CONTEXT_RU,
RUNNER_CONTEXTS,
RUNNER_MODE_RU,
RUNNER_MODES,
RUNNER_WORKSPACE_RU,
RUNNER_WORKSPACES,
type RunnerSettings,
type RunnerSettingsPatch,
type RunnerTaskView,
type SessionView,
} from "@peppa/core";
import { promoteRunnerAction, queueOnRunnerAction, renameRunnerAction, setRunnerSettingsAction, startFreeRunnerAction } from "@/app/(app)/actions";
import { Hidden } from "@/components/cards";
import { Key, StatusBadge } from "@/components/common";
import { PanelSection } from "@/components/ActivityAside";
import { Button } from "@/components/ui/button";
import { Input, Label, NativeSelect, Textarea } from "@/components/ui/input";
import { VoiceInput } from "@/components/VoiceInput";
/** Free runner form: project (or fixed), name, agent, prompt with voice input. */
export function NewRunnerForm({ projects, projectId, back }: { projects?: Array<{ id: number; key: string; name: string }>; projectId?: number; back: string }) {
const promptId = `runner-prompt-${projectId ?? "any"}`;
return (
<form action={startFreeRunnerAction} className="flex flex-col gap-3">
<Hidden fields={{ back, ...(projectId ? { projectId } : {}) }} />
<p className="m-0 text-sm text-muted-foreground">Агент в tmux без стори: разобраться, попробовать, спросить. Пинги после ходов не приходят — раннером управляешь ты в терминале. Получилась работа — «Оформить стори».</p>
{!projectId && (
<Label>
Проект
<NativeSelect name="projectId" required defaultValue="" className="h-8">
<option value="" disabled>Выбери проект</option>
{projects?.map((p) => (
<option key={p.id} value={p.id}>
{p.key} — {p.name}
</option>
))}
</NativeSelect>
</Label>
)}
<Label>
Имя
<Input name="name" placeholder="Новый терминал" maxLength={80} className="h-8" />
</Label>
<Label>
Агент
<NativeSelect name="provider" defaultValue="" className="h-8">
<option value="">По пулу проекта (иначе Claude Code)</option>
<option value="claude">Claude Code</option>
<option value="codex">Codex</option>
<option value="opencode">OpenCode</option>
</NativeSelect>
</Label>
<Label htmlFor={promptId}>Промпт (необязательно)</Label>
<Textarea id={promptId} name="prompt" rows={4} placeholder="Что сделать агенту" />
<div className="flex flex-wrap gap-2">
<Button>
<SquareTerminal />
Запустить
</Button>
<VoiceInput target={promptId} />
</div>
</form>
);
}
const inherit = (label: string) => `Как выше: ${label}`;
/** «⋯ Настройки проекта → Раннеры»: overrides of the project; empty = system. */
export function RunnerSettingsPanel({ projectId, own, inherited, back }: { projectId: number; own: RunnerSettingsPatch; inherited: RunnerSettings; back: string }) {
return (
<form action={setRunnerSettingsAction} className="flex flex-col gap-3">
<Hidden fields={{ projectId, back }} />
<Label>
Раннер и стори
<NativeSelect name="mode" defaultValue={own.mode ?? ""} className="h-8">
<option value="">{inherit(RUNNER_MODE_RU[inherited.mode])}</option>
{RUNNER_MODES.map((m) => (
<option key={m} value={m}>{RUNNER_MODE_RU[m]}</option>
))}
</NativeSelect>
</Label>
<Label>
Переход к следующей стори (для очереди)
<NativeSelect name="context" defaultValue={own.context ?? ""} className="h-8">
<option value="">{inherit(RUNNER_CONTEXT_RU[inherited.context])}</option>
{RUNNER_CONTEXTS.map((m) => (
<option key={m} value={m}>{RUNNER_CONTEXT_RU[m]}</option>
))}
</NativeSelect>
</Label>
<Label>
Рабочая копия
<NativeSelect name="workspace" defaultValue={own.workspace ?? ""} className="h-8">
<option value="">{inherit(RUNNER_WORKSPACE_RU[inherited.workspace])}</option>
{RUNNER_WORKSPACES.map((m) => (
<option key={m} value={m}>{RUNNER_WORKSPACE_RU[m]}</option>
))}
</NativeSelect>
</Label>
<Label>
Лимит раннеров проекта (пусто — {inherited.maxRunners ? `как выше: ${inherited.maxRunners}` : "без лимита"}, 0 — без лимита)
<Input name="maxRunners" type="number" min={0} max={50} defaultValue={own.maxRunners === undefined ? "" : (own.maxRunners ?? 0)} className="h-8" />
</Label>
<p className="m-0 text-xs text-muted-foreground">
Очередь: стори достаётся свободному раннеру, при лимите ждёт в очереди; «Принять» не останавливает раннер, а даёт ему следующую стори. Свой worktree: у каждого раннера своя копия и ветка <code>task/…</code>, параллельные стори не мешают друг другу.
</p>
<Button size="sm" className="self-start">Сохранить</Button>
</form>
);
}
/** Story page: mode/context for this story only. */
export function TaskRunnerSettings({ taskKey, own, inherited, back }: { taskKey: string; own: { mode?: string; context?: string }; inherited: RunnerSettings; back: string }) {
return (
<form action={setRunnerSettingsAction} className="flex flex-wrap items-end gap-2">
<Hidden fields={{ taskKey, back }} />
<Label className="flex-[1_1_12rem]">
Раннер этой стори
<NativeSelect name="mode" defaultValue={own.mode ?? ""} className="h-8">
<option value="">{inherit(RUNNER_MODE_RU[inherited.mode])}</option>
{RUNNER_MODES.map((m) => (
<option key={m} value={m}>{RUNNER_MODE_RU[m]}</option>
))}
</NativeSelect>
</Label>
<Label className="flex-[1_1_12rem]">
Контекст
<NativeSelect name="context" defaultValue={own.context ?? ""} className="h-8">
<option value="">{inherit(RUNNER_CONTEXT_RU[inherited.context])}</option>
{RUNNER_CONTEXTS.map((m) => (
<option key={m} value={m}>{RUNNER_CONTEXT_RU[m]}</option>
))}
</NativeSelect>
</Label>
<Button size="sm" variant="outline">Сохранить</Button>
</form>
);
}
/** Runner details: name, stories (history and queue), «Оформить стори» for a free runner. */
export function RunnerStories({ s, history, queue, back }: { s: SessionView; history: RunnerTaskView[]; queue: RunnerTaskView[]; back: string }) {
const alive = !["done", "failed"].includes(s.status);
return (
<>
<form action={renameRunnerAction} className="flex gap-2">
<Hidden fields={{ sessionId: s.id, back }} />
<Input name="name" defaultValue={s.name ?? ""} placeholder="Имя раннера" aria-label="Имя раннера" maxLength={80} className="h-8" />
<Button size="sm" variant="outline">Переименовать</Button>
</form>
{s.workspacePath && <p className="m-0 font-mono text-xs break-all text-muted-foreground">worktree: {s.workspacePath}</p>}
{!s.taskId && (
<PanelSection title="Оформить стори">
<form action={promoteRunnerAction} className="flex flex-col gap-2">
<Hidden fields={{ sessionId: s.id, back }} />
<Input name="title" required minLength={3} placeholder="Название стори" aria-label="Название стори" className="h-8" />
<Textarea name="description" rows={2} placeholder="Описание (необязательно)" aria-label="Описание стори" />
<Button size="sm" className="self-start">
<Plus />
Оформить стори
</Button>
</form>
</PanelSection>
)}
{(history.length > 0 || queue.length > 0 || (alive && s.taskId)) && (
<PanelSection title="Стори раннера" count={history.length + queue.length}>
<ol className="m-0 flex list-none flex-col gap-1.5 p-0 text-sm">
{queue.map((r) => (
<li key={r.id} className="flex items-center gap-2">
<span className="font-mono text-xs text-muted-foreground">в очереди{r.front ? " (первой)" : ""}</span>
<Key href={`/t/${r.taskKey}`}>{r.taskKey}</Key>
<span className="truncate">{r.taskTitle}</span>
</li>
))}
{history.map((r) => (
<li key={r.id} className="flex items-center gap-2">
<span className="font-mono text-xs text-muted-foreground">{r.status === "active" ? "ведёт" : "вёл"}</span>
<Key href={`/t/${r.taskKey}`}>{r.taskKey}</Key>
<span className="min-w-0 truncate">{r.taskTitle}</span>
<span className="ml-auto">
<StatusBadge status={r.taskStatus} />
</span>
</li>
))}
</ol>
{alive && s.taskId && (
<form action={queueOnRunnerAction} className="flex gap-2">
<Hidden fields={{ sessionId: s.id, back }} />
<Input name="taskKey" required placeholder={`${s.projectKey}-N`} aria-label="Стори в очередь" className="h-8" />
<Button size="sm" variant="outline">В очередь</Button>
</form>
)}
</PanelSection>
)}
</>
);
}