mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 11:07:40 +01:00
fix(control): never lose a custom prompt — draft persistence + inline error
Incident 2026-05-20: user sent a custom prompt from the mobile site, the
request silently failed (or the global page-top error went unseen because
the user was scrolled to a project card), and the typed text was lost.
Queried pending_commands — zero unclaimed, zero stuck, zero failed in the
last hour, so the request never reached the server. The user thought it
worked.
Root causes (all of them):
1. sendCustom had no per-card error surface — only ControlPanel set a
global error at the top of the page, invisible on mobile when
scrolled.
2. ControlPanel's onInject catch SWALLOWED the throw so children had no
way to react.
3. The custom input wasn't persisted anywhere — a failed send + page
close meant typed text was gone.
4. PromptInput rendered no failure state at all.
Defense in depth (all in one commit):
- src/lib/draft-storage.ts (new): per-tab localStorage SSOT
(getDraft / setDraft / clearDraft). Survives refresh, tab close, failed
send. Best-effort with try/catch around every localStorage call.
- useProjectCardActions: lazy-init custom from getDraft, wrap setCustom
to write to localStorage on every keystroke, sendCustom/sendText
catch+expose sendError and only clearDraft on confirmed success.
Exports sendError + clearSendError.
- ControlPanel: onInject / onRunWithBrain / onRunCustomPrompt now
setError(global) AND re-throw so child cards can also display inline.
- ProjectCard + project-intent-panel: thread sendError + onClearSendError
through to PromptInput.
- PromptInput: red inline error banner above the textarea — "Send failed
/ <reason> / Your text is preserved below — fix the issue and tap Send
again" with a dismiss X. Visible right where the user is looking.
Outcome: a failed send is now impossible to miss AND impossible to lose
the typed text from. Every layer of the failure path has its own guard.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
6 files changed
+142
-7
No files matched your search
@@ -94,15 +94,29 @@ export function ControlPanel() {
|
||||
setQueuedNotice(`Command queued — local daemon will execute it for ${tab}`);
|
||||
setTimeout(() => setQueuedNotice(null), 6000);
|
||||
}
|
||||
} catch (err) { setError(err instanceof Error ? err.message : "Injection failed"); }
|
||||
} catch (err) {
|
||||
// Set the global error (renders at the top of the page) AND re-throw
|
||||
// so the per-card sendError state in useProjectCardActions can render
|
||||
// an inline error near the send button. The global banner is invisible
|
||||
// when a mobile user is scrolled down to a project card — the inline
|
||||
// error is the surface they're guaranteed to see.
|
||||
setError(err instanceof Error ? err.message : "Injection failed");
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
onRunWithBrain: async (projectState: ProjectState, intent: OrchestrationTaskIntentId) => {
|
||||
try { await runWithBrain(projectState, intent); }
|
||||
catch (err) { setError(err instanceof Error ? err.message : "Failed to run task"); }
|
||||
catch (err) {
|
||||
setError(err instanceof Error ? err.message : "Failed to run task");
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
onRunCustomPrompt: async (projectState: ProjectState, prompt: string, ag: string) => {
|
||||
try { await runCustomPrompt(projectState, prompt, ag); }
|
||||
catch (err) { setError(err instanceof Error ? err.message : "Failed to run prompt"); }
|
||||
catch (err) {
|
||||
setError(err instanceof Error ? err.message : "Failed to run prompt");
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
onDeleted: () => { refresh(true); },
|
||||
onProfileSaved: () => { refresh(true); },
|
||||
|
||||
@@ -94,7 +94,7 @@ export function ProjectCard({
|
||||
|
||||
const {
|
||||
sending, custom, setCustom, customFocused, setCustomFocused,
|
||||
merging, preloadedDispatch,
|
||||
merging, preloadedDispatch, sendError, clearSendError,
|
||||
sendCustom, sendText, sessionHealthBlocksQueue, sendIntent, send,
|
||||
handleAutoInject, handleSendFromQueue, handleMergeQueue,
|
||||
} = useProjectCardActions({
|
||||
@@ -189,6 +189,8 @@ export function ProjectCard({
|
||||
isRunning={display.isRunning}
|
||||
autoContinueEnabled={autoContinueEnabled}
|
||||
sending={sending}
|
||||
sendError={sendError}
|
||||
onClearSendError={clearSendError}
|
||||
custom={custom}
|
||||
queue={queue}
|
||||
bannerActive={display.isClosed || display.isReady || display.isOrchestrationReady}
|
||||
|
||||
@@ -19,6 +19,8 @@ export function IntentButtonPanel({
|
||||
isRunning,
|
||||
autoContinueEnabled,
|
||||
sending,
|
||||
sendError,
|
||||
onClearSendError,
|
||||
custom,
|
||||
queue = [],
|
||||
bannerActive,
|
||||
@@ -42,6 +44,10 @@ export function IntentButtonPanel({
|
||||
isRunning: boolean;
|
||||
autoContinueEnabled: boolean;
|
||||
sending: string | null;
|
||||
/** Inline error from the last sendCustom/sendText/sendIntent attempt — surfaced near the send button. */
|
||||
sendError?: string | null;
|
||||
/** Dismiss the inline error (called from the dismiss button on PromptInput). */
|
||||
onClearSendError?: () => void;
|
||||
custom: string;
|
||||
queue?: string[];
|
||||
bannerActive?: boolean;
|
||||
@@ -80,6 +86,7 @@ export function IntentButtonPanel({
|
||||
|
||||
const inputProps = {
|
||||
custom, listening, processing, micError, sending, waveformBars, recordingSeconds, maxRecordingSeconds,
|
||||
sendError, onClearSendError,
|
||||
onCustomChange, onCustomFocusChange, toggleMic,
|
||||
showQueue: !!onEnqueueCustom,
|
||||
onSendCustom: handleSendCustom,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { Loader2, Send, Mic, ListPlus, Pause, Play } from "lucide-react";
|
||||
import { AlertCircle, Loader2, Send, Mic, ListPlus, Pause, Play, X } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
export function PromptInput({
|
||||
@@ -9,6 +9,8 @@ export function PromptInput({
|
||||
processing,
|
||||
micError,
|
||||
sending,
|
||||
sendError,
|
||||
onClearSendError,
|
||||
placeholder,
|
||||
showQueue,
|
||||
waveformBars,
|
||||
@@ -29,6 +31,12 @@ export function PromptInput({
|
||||
processing: boolean;
|
||||
micError: string;
|
||||
sending: string | null;
|
||||
/** Inline error banner shown above the input when the last send attempt
|
||||
* failed. The draft text is preserved (custom state survives), so the user
|
||||
* can retry from the same input. */
|
||||
sendError?: string | null;
|
||||
/** Dismiss the inline error banner. */
|
||||
onClearSendError?: () => void;
|
||||
placeholder: string;
|
||||
showQueue?: boolean;
|
||||
waveformBars?: number[];
|
||||
@@ -63,6 +71,30 @@ export function PromptInput({
|
||||
|
||||
return (
|
||||
<div className="overflow-hidden rounded-2xl border border-border-default bg-surface-raised">
|
||||
{sendError && (
|
||||
<div className="flex items-start gap-2 border-b border-status-negative/30 bg-status-negative-subtle px-3 py-2">
|
||||
<AlertCircle className="mt-0.5 h-3.5 w-3.5 shrink-0 text-status-negative" />
|
||||
<div className="min-w-0 flex-1">
|
||||
<p className="text-xs font-medium text-status-negative">Send failed</p>
|
||||
<p className="mt-0.5 truncate text-xs text-status-negative/80" title={sendError}>
|
||||
{sendError}
|
||||
</p>
|
||||
<p className="mt-1 text-micro text-text-tertiary">
|
||||
Your text is preserved below — fix the issue and tap Send again.
|
||||
</p>
|
||||
</div>
|
||||
{onClearSendError && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClearSendError}
|
||||
aria-label="Dismiss error"
|
||||
className="ui-icon-action shrink-0 hover:text-text-primary"
|
||||
>
|
||||
<X className="h-3 w-3" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<div className="relative">
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
|
||||
@@ -8,6 +8,7 @@ import type { OrchestrationTaskIntentId } from "@/lib/orchestration";
|
||||
import { mapClaudePromptToIntent } from "@/lib/orchestration";
|
||||
import { isAutoContinueEnabledSync } from "@/lib/control-storage";
|
||||
import type { DispatchResult } from "@/app/api/control/dispatch/route";
|
||||
import { clearDraft, getDraft, setDraft } from "@/lib/draft-storage";
|
||||
|
||||
export function useProjectCardActions({
|
||||
project,
|
||||
@@ -35,10 +36,24 @@ export function useProjectCardActions({
|
||||
isOnlyReady: boolean;
|
||||
}) {
|
||||
const [sending, setSending] = useState<string | null>(null);
|
||||
const [custom, setCustom] = useState("");
|
||||
// Lazy-init from localStorage draft so a failed send / page reload / tab
|
||||
// close doesn't drop the user's typed prompt. clearDraft is called only on
|
||||
// confirmed-successful sendCustom / sendText. See incident 2026-05-20:
|
||||
// mobile user sent from phone, request errored, draft lost.
|
||||
const [custom, _setCustom] = useState<string>(() => getDraft(project.tab));
|
||||
const setCustom = useCallback((next: string) => {
|
||||
_setCustom(next);
|
||||
setDraft(project.tab, next);
|
||||
}, [project.tab]);
|
||||
const [customFocused, setCustomFocused] = useState(false);
|
||||
const [merging, setMerging] = useState(false);
|
||||
const [preloadedDispatch, setPreloadedDispatch] = useState<DispatchResult | null>(null);
|
||||
// sendError surfaces dispatch failures inline near the send button — the
|
||||
// global ControlPanel error renders above the project cards and is invisible
|
||||
// on mobile when the user is scrolled down to a specific card. Inline error
|
||||
// means a failed send is impossible to miss.
|
||||
const [sendError, setSendError] = useState<string | null>(null);
|
||||
const clearSendError = useCallback(() => setSendError(null), []);
|
||||
|
||||
// Pre-fetch dispatch decision as soon as the ready banner appears.
|
||||
// Note 2026-05-20: previously gated on queue.length > 0, which short-
|
||||
@@ -81,10 +96,17 @@ export function useProjectCardActions({
|
||||
const sendCustom = async () => {
|
||||
if (!custom.trim()) return;
|
||||
setSending("custom");
|
||||
setSendError(null);
|
||||
setDismissed(true);
|
||||
try {
|
||||
await onInject(project.tab, undefined, custom.trim());
|
||||
// Only clear input + draft AFTER confirmed success. If onInject throws
|
||||
// (the parent in ControlPanel re-throws via setError), the text stays.
|
||||
setCustom("");
|
||||
} catch (err) {
|
||||
// onInject from ControlPanel already swallows + calls setError(global).
|
||||
// We still defensively capture here in case the wrapper path changes.
|
||||
setSendError(err instanceof Error ? err.message : "Send failed");
|
||||
} finally {
|
||||
setSending(null);
|
||||
}
|
||||
@@ -93,9 +115,15 @@ export function useProjectCardActions({
|
||||
const sendText = useCallback(async (text: string) => {
|
||||
if (!text.trim()) return;
|
||||
setSending("custom");
|
||||
setSendError(null);
|
||||
setDismissed(true);
|
||||
try {
|
||||
await onInject(project.tab, undefined, text.trim());
|
||||
// Belt-and-suspenders: sendText bypasses setCustom (the draft auto-clear
|
||||
// path), so explicit clearDraft after successful send.
|
||||
clearDraft(project.tab);
|
||||
} catch (err) {
|
||||
setSendError(err instanceof Error ? err.message : "Send failed");
|
||||
} finally {
|
||||
setSending(null);
|
||||
}
|
||||
@@ -221,7 +249,7 @@ export function useProjectCardActions({
|
||||
} catch { /* ignore */ } finally {
|
||||
setMerging(false);
|
||||
}
|
||||
}, [queue, clearQueue]);
|
||||
}, [queue, clearQueue, setCustom]);
|
||||
|
||||
// Keyboard: 1–9 dispatch prompt slots when this is the sole ready project on the page.
|
||||
const sendRef = useRef(send);
|
||||
@@ -258,6 +286,8 @@ export function useProjectCardActions({
|
||||
setCustomFocused,
|
||||
merging,
|
||||
preloadedDispatch,
|
||||
sendError,
|
||||
clearSendError,
|
||||
sendCustom,
|
||||
sendText,
|
||||
sessionHealthBlocksQueue,
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Per-project draft persistence for the custom prompt input.
|
||||
*
|
||||
* Problem we're solving: if a /api/inject POST fails silently (auth expiry,
|
||||
* 500, network drop), the user's typed text would be lost when they navigate
|
||||
* away or close the page. Persisting to localStorage on every keystroke means
|
||||
* the draft survives a failed submit, a tab close, or a refresh — they can
|
||||
* always recover what they tried to send.
|
||||
*
|
||||
* Cleared explicitly on a confirmed-successful send (200 response from
|
||||
* /api/inject). Reads return "" when localStorage is unavailable (SSR or
|
||||
* private-browsing) — best-effort, no error states bubble out.
|
||||
*/
|
||||
|
||||
const KEY_PREFIX = "cockpit:draft:";
|
||||
|
||||
function key(tab: string): string {
|
||||
return `${KEY_PREFIX}${tab}`;
|
||||
}
|
||||
|
||||
export function getDraft(tab: string): string {
|
||||
if (typeof window === "undefined") return "";
|
||||
try {
|
||||
return window.localStorage.getItem(key(tab)) ?? "";
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
export function setDraft(tab: string, text: string): void {
|
||||
if (typeof window === "undefined") return;
|
||||
try {
|
||||
if (text.trim()) {
|
||||
window.localStorage.setItem(key(tab), text);
|
||||
} else {
|
||||
window.localStorage.removeItem(key(tab));
|
||||
}
|
||||
} catch {
|
||||
/* localStorage full, denied, or unsupported — fail silent */
|
||||
}
|
||||
}
|
||||
|
||||
export function clearDraft(tab: string): void {
|
||||
if (typeof window === "undefined") return;
|
||||
try {
|
||||
window.localStorage.removeItem(key(tab));
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user