Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion e2e/global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as os from 'os';

const PID_FILE = '/tmp/autopr-e2e.pid';
export const FIXTURES_FILE = '/tmp/autopr-e2e-fixtures.json';
const AUTO_PRD_PATH = process.env.AUTO_PRD_PATH || '/usr/local/bin/auto-prd';

export interface E2EFixtures {
repoDir: string;
Expand Down Expand Up @@ -118,7 +119,7 @@ providers:
}

async function startDaemon(homeDir: string): Promise<void> {
const daemon = spawn('/usr/local/bin/auto-prd', [], {
const daemon = spawn(AUTO_PRD_PATH, [], {
env: { ...process.env, HOME: homeDir },
stdio: ['ignore', 'pipe', 'pipe'],
detached: false,
Expand Down
2 changes: 1 addition & 1 deletion e2e/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "ES2022",
"module": "commonjs",
"lib": ["ES2022"],
"lib": ["ES2022", "DOM"],
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true,
Expand Down
194 changes: 133 additions & 61 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { AddTicketDialog } from "./AddTicketDialog";
import { DiscoverTicketsModal } from "./DiscoverTicketsModal";
import { ExecutionLogsModal } from "./ExecutionLogsModal";
import { extractOpenQuestions, formatFeedbackMessage } from "./investigationFeedback";
import { TicketDetailPanel } from "./TicketDetailPanel";
import { TicketList } from "./TicketList";
import {
Expand All @@ -38,7 +39,9 @@ export function App() {
const [details, setDetails] = useState<TicketDetails | null>(null);
const [selectedRunId, setSelectedRunId] = useState("");
const [selectedArtifactContent, setSelectedArtifactContent] = useState("");
const [feedbackMessage, setFeedbackMessage] = useState("");
const [currentFeedbackArtifactContent, setCurrentFeedbackArtifactContent] = useState("");
const [questionAnswers, setQuestionAnswers] = useState<Record<string, string>>({});
const [generalFeedback, setGeneralFeedback] = useState("");
const [activeJobId, setActiveJobId] = useState("");
const [activeJob, setActiveJob] = useState<Job | null>(null);
const [loading, setLoading] = useState(false);
Expand All @@ -64,14 +67,80 @@ export function App() {
const availableRepoPaths = useMemo(() => knownRepoPaths(repositoryOptions, tickets), [repositoryOptions, tickets]);
const stateRuns = useMemo(() => stateRunsFromDetails(details), [details]);
const feedbackAction = useMemo(() => getFeedbackAction(details, selectedSummary), [details, selectedSummary]);
const currentRunId = details?.state.current_run_id ?? "";
const currentRun = useMemo(
() => stateRuns.find((run) => run.id === currentRunId) ?? null,
[currentRunId, stateRuns]
);
const openQuestions = useMemo(
() => extractOpenQuestions(currentFeedbackArtifactContent),
[currentFeedbackArtifactContent]
);

const selectedSummaryRef = useRef<TicketSummary | null>(null);
const activeJobIdRef = useRef("");
const showLogsModalRef = useRef(false);
const fullRefreshScheduledRef = useRef(false);
const prevLastRunIdRef = useRef("");
const handleServerEventRef = useRef<(evt: ServerEvent) => Promise<void>>(async () => {});
const reconnectErrorMessage = "event stream connection lost; reconnecting";

async function handleServerEvent(evt: ServerEvent) {
const selected = selectedSummaryRef.current;
const trackedJobID = activeJobIdRef.current;
if (evt.type === "job" && evt.job_id && trackedJobID && evt.job_id === trackedJobID) {
const status = evt.status ?? "";
if (status === "failed") {
try {
const job = await getJob(evt.job_id);
setActiveJob(job);
} catch (err) {
setError(err instanceof Error ? err.message : "failed to refresh job");
} finally {
setActiveJobId("");
}
} else if (status === "done") {
setActiveJob(null);
setActiveJobId("");
} else if (status === "queued" || status === "running") {
setActiveJob((current) => {
if (!current) {
return current;
}
return { ...current, status };
});
}
}

let needsFullRefresh = evt.type === "repo_tickets_synced";
setTickets((current) => {
const ticketUpdate = applyTicketEvent(current, evt);
if (ticketUpdate.needsFullRefresh) {
needsFullRefresh = true;
}
return ticketUpdate.tickets;
});
if (needsFullRefresh) {
scheduleFullRefresh();
}

if (
selected &&
evt.type === "ticket_updated" &&
evt.repo_path === selected.repo_path &&
evt.ticket_number === selected.ticket_number
) {
if ((evt.status ?? "") !== "running" && activeJobIdRef.current) {
activeJobIdRef.current = "";
setActiveJobId("");
}
await refreshTicketDetails(selected.repo_path, selected.ticket_number, false);
if (showLogsModalRef.current) {
await refreshExecutionLogs(selected.repo_path, selected.ticket_number);
}
}
}

useEffect(() => {
selectedSummaryRef.current = selectedSummary;
}, [selectedSummary]);
Expand All @@ -84,6 +153,10 @@ export function App() {
showLogsModalRef.current = showLogsModal;
}, [showLogsModal]);

useEffect(() => {
handleServerEventRef.current = handleServerEvent;
});

useEffect(() => {
void refreshTickets();
void refreshRepositories();
Expand All @@ -92,7 +165,7 @@ export function App() {
.catch(() => { setDiscoverConfigured(false); });
const stream = connectEvents(
(evt) => {
void handleServerEvent(evt);
void handleServerEventRef.current(evt);
},
() => {
setError(reconnectErrorMessage);
Expand All @@ -109,14 +182,17 @@ export function App() {
setDetails(null);
setSelectedRunId("");
setSelectedArtifactContent("");
setCurrentFeedbackArtifactContent("");
setQuestionAnswers({});
setGeneralFeedback("");
setArtifactLoading(false);
setShowLogsModal(false);
setExecutionLogs([]);
setExecutionLogsLoading(false);
return;
}
void refreshTicketDetails(selectedSummary.repo_path, selectedSummary.ticket_number);
}, [selectedSummary?.repo_path, selectedSummary?.ticket_number]);
}, [selectedSummary]);

useEffect(() => {
if (stateRuns.length === 0) {
Expand Down Expand Up @@ -182,6 +258,40 @@ export function App() {
};
}, [selectedRunId, selectedSummary, stateRuns]);

useEffect(() => {
if (!selectedSummary || !currentRun) {
setCurrentFeedbackArtifactContent("");
return;
}
const artifactRef = currentRun.artifact_ref || currentRun.log_ref;
if (!artifactRef) {
setCurrentFeedbackArtifactContent("");
return;
}

let cancelled = false;
void getArtifact(selectedSummary.repo_path, selectedSummary.ticket_number, artifactRef)
.then((content) => {
if (!cancelled) {
setCurrentFeedbackArtifactContent(content);
}
})
.catch((err) => {
if (!cancelled) {
setError(err instanceof Error ? err.message : "failed to load feedback artifact");
setCurrentFeedbackArtifactContent("");
}
});
return () => {
cancelled = true;
};
}, [currentRun, selectedSummary]);

useEffect(() => {
setQuestionAnswers({});
setGeneralFeedback("");
}, [selectedSummary?.repo_path, selectedSummary?.ticket_number, currentRunId]);

useEffect(() => {
if (!showLogsModal || !selectedSummary) {
return;
Expand All @@ -207,7 +317,7 @@ export function App() {
return () => {
cancelled = true;
};
}, [showLogsModal, selectedSummary?.repo_path, selectedSummary?.ticket_number]);
}, [showLogsModal, selectedSummary]);

async function refreshTickets(showLoader = true) {
if (showLoader) {
Expand Down Expand Up @@ -263,58 +373,6 @@ export function App() {
}
}

async function handleServerEvent(evt: ServerEvent) {
const selected = selectedSummaryRef.current;
const trackedJobID = activeJobIdRef.current;
if (evt.type === "job" && evt.job_id && trackedJobID && evt.job_id === trackedJobID) {
const status = evt.status ?? "";
if (status === "failed") {
try {
const job = await getJob(evt.job_id);
setActiveJob(job);
} catch (err) {
setError(err instanceof Error ? err.message : "failed to refresh job");
} finally {
setActiveJobId("");
}
} else if (status === "done") {
setActiveJob(null);
setActiveJobId("");
} else if (status === "queued" || status === "running") {
setActiveJob((current) => {
if (!current) {
return current;
}
return { ...current, status };
});
}
}

let needsFullRefresh = evt.type === "repo_tickets_synced";
setTickets((current) => {
const ticketUpdate = applyTicketEvent(current, evt);
if (ticketUpdate.needsFullRefresh) {
needsFullRefresh = true;
}
return ticketUpdate.tickets;
});
if (needsFullRefresh) {
scheduleFullRefresh();
}

if (
selected &&
evt.type === "ticket_updated" &&
evt.repo_path === selected.repo_path &&
evt.ticket_number === selected.ticket_number
) {
await refreshTicketDetails(selected.repo_path, selected.ticket_number, false);
if (showLogsModalRef.current) {
await refreshExecutionLogs(selected.repo_path, selected.ticket_number);
}
}
}

function scheduleFullRefresh() {
if (fullRefreshScheduledRef.current) {
return;
Expand All @@ -330,6 +388,7 @@ export function App() {
setError("");
try {
const accepted = await fn();
activeJobIdRef.current = accepted.job_id;
setActiveJobId(accepted.job_id);
setActiveJob(null);
return true;
Expand Down Expand Up @@ -402,23 +461,32 @@ export function App() {
}

function submitFeedback() {
if (!selectedSummary || !feedbackAction || !feedbackMessage.trim()) {
if (!selectedSummary || !feedbackAction) {
return;
}
const message = formatFeedbackMessage(openQuestions, questionAnswers, generalFeedback);
if (!message.trim()) {
return;
}
void queueAction(() =>
applyAction(
selectedSummary.repo_path,
selectedSummary.ticket_number,
feedbackAction.label,
feedbackMessage
message
)
).then((ok) => {
if (ok) {
setFeedbackMessage("");
setQuestionAnswers({});
setGeneralFeedback("");
}
});
}

function updateQuestionAnswer(index: number, value: string) {
setQuestionAnswers((current) => ({ ...current, [String(index)]: value }));
}

function applyNamedAction(label: string) {
if (!selectedSummary) {
return;
Expand Down Expand Up @@ -521,9 +589,13 @@ export function App() {
selectedArtifactContent={selectedArtifactContent}
artifactLoading={artifactLoading}
feedbackAction={feedbackAction}
feedbackMessage={feedbackMessage}
openQuestions={feedbackAction ? openQuestions : []}
questionAnswers={questionAnswers}
generalFeedback={generalFeedback}
isRunning={!!activeJobId}
onSelectRun={setSelectedRunId}
onFeedbackMessageChange={setFeedbackMessage}
onQuestionAnswerChange={updateQuestionAnswer}
onGeneralFeedbackChange={setGeneralFeedback}
onSubmitFeedback={submitFeedback}
onApplyAction={applyNamedAction}
onOpenLogs={() => setShowLogsModal(true)}
Expand Down
Loading
Loading