Workflow-step REVISE retries, pause→todo handoffs, and the context-overflow fresh-session requeue were all routing tasks back to `todo` before returning to `in-progress`. The default reopen-to-todo path reset every step to pending and rewrote PROMPT.md checkboxes, so each retry restarted from step 0 even when earlier steps had already been done — the symptom seen on FN-2978, where every workflow REVISE or pause cycle wiped the task's progress. - Add `preserveResumeState` to `TaskStore.moveTask`. When set, skip `resetAllStepsToPending` + `resetPromptCheckboxes` and keep `worktree` and `executionStartedAt` so the resumed run reattaches to the same checkout. `status`, `error`, and `blockedBy` still clear. - Use it on the workflow-rerun bounce, the three pause-graceful handoffs, and the context-overflow requeue. The agent-terminated pause path still discards (it nukes worktree+branch by design). - Context-overflow requeue clears `sessionFile` synchronously in the awaited `updateTask` immediately before `moveTask`, so the next dispatch cannot reopen the saturated session via a stale pointer. - `fn_task_update` no longer silently regresses `done`/`skipped` steps to `in-progress`, no longer captures a stale rewind checkpoint when it does, and tells the agent honestly when a regression is ignored. - Mobile chat keyboard: ChatView/QuickChatFAB gate layout on the new `keyboardOpen` flag so focused-input + viewport-shrink iOS cases still adjust when the computed overlap is zero. Tests: - New `preserveResumeState` coverage in store.test.ts; updated workflow-rerun + pause-graceful assertions in executor.test.ts. - Restructured the previously-flaky "routes exhausted prompt-mode workflow hard failures" test to drive the bounce inline; passes in isolation and in the wider workflow/pause/context sweep (59/59). - Added regression tests in ChatView.test.tsx and QuickChatFAB.test.tsx for the iOS last-resort `keyboardOpen=true, keyboardOverlap=0` case. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
144 lines
4.6 KiB
TypeScript
144 lines
4.6 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
|
|
const IOS_FALLBACK_MIN_GAP_PX = 30;
|
|
const IOS_FALLBACK_MIN_FOCUSED_GAP_PX = 16;
|
|
const IOS_VIEWPORT_SHRINK_MIN_PX = 16;
|
|
|
|
/** Whether the current device is likely mobile (touch-primary, small viewport). */
|
|
function isMobileDevice(): boolean {
|
|
if (typeof window === "undefined") return false;
|
|
const hasTouchScreen =
|
|
"ontouchstart" in window || navigator.maxTouchPoints > 0;
|
|
const isNarrow = window.innerWidth <= 768;
|
|
return hasTouchScreen && isNarrow;
|
|
}
|
|
|
|
/**
|
|
* Baseline viewport height captured while keyboard is likely closed.
|
|
* Kept as max-observed value to recover if first sample was keyboard-open.
|
|
*/
|
|
let _baselineViewportHeight: number | null = null;
|
|
|
|
function getBaselineViewportHeight(): number {
|
|
if (_baselineViewportHeight === null) {
|
|
_baselineViewportHeight = window.visualViewport?.height ?? window.innerHeight;
|
|
}
|
|
return _baselineViewportHeight;
|
|
}
|
|
|
|
function updateBaselineViewportHeight(nextHeight: number): void {
|
|
const current = getBaselineViewportHeight();
|
|
if (nextHeight > current) {
|
|
_baselineViewportHeight = nextHeight;
|
|
}
|
|
}
|
|
|
|
function isKeyboardFocusableElement(el: Element | null): boolean {
|
|
if (!el) return false;
|
|
if (el instanceof HTMLTextAreaElement) return true;
|
|
if (el instanceof HTMLInputElement) {
|
|
const nonTextTypes = new Set(["checkbox", "radio", "button", "submit", "reset", "file", "range", "color", "hidden"]);
|
|
return !nonTextTypes.has(el.type);
|
|
}
|
|
return el instanceof HTMLElement && el.isContentEditable;
|
|
}
|
|
|
|
function getKeyboardMetrics(): { overlap: number; open: boolean; vvHeight: number | null } {
|
|
if (typeof window === "undefined" || !window.visualViewport) {
|
|
return { overlap: 0, open: false, vvHeight: null };
|
|
}
|
|
|
|
const vv = window.visualViewport;
|
|
const focused = isKeyboardFocusableElement(document.activeElement);
|
|
|
|
// Only refresh baseline while keyboard is likely closed.
|
|
if (!focused) {
|
|
updateBaselineViewportHeight(vv.height);
|
|
}
|
|
|
|
// Android/Chrome style overlap.
|
|
const chromeOverlap = Math.max(0, window.innerHeight - vv.offsetTop - vv.height);
|
|
if (chromeOverlap > 0) {
|
|
return { overlap: chromeOverlap, open: true, vvHeight: vv.height };
|
|
}
|
|
|
|
// iOS fallback (window.innerHeight shrinks with keyboard).
|
|
const baselineHeight = getBaselineViewportHeight();
|
|
const gap = Math.max(0, baselineHeight - vv.offsetTop - vv.height);
|
|
|
|
if (gap >= IOS_FALLBACK_MIN_GAP_PX) {
|
|
return { overlap: gap, open: true, vvHeight: vv.height };
|
|
}
|
|
|
|
if (gap >= IOS_FALLBACK_MIN_FOCUSED_GAP_PX && focused) {
|
|
return { overlap: gap, open: true, vvHeight: vv.height };
|
|
}
|
|
|
|
// Last-resort signal: focused input + meaningful viewport shrink.
|
|
const viewportShrink = Math.max(0, baselineHeight - vv.height);
|
|
if (focused && viewportShrink >= IOS_VIEWPORT_SHRINK_MIN_PX) {
|
|
return { overlap: 0, open: true, vvHeight: vv.height };
|
|
}
|
|
|
|
return { overlap: 0, open: false, vvHeight: null };
|
|
}
|
|
|
|
/** Reset cached viewport baseline. Exported for tests only. */
|
|
export function _resetInitialViewportHeight(): void {
|
|
_baselineViewportHeight = null;
|
|
}
|
|
|
|
interface UseMobileKeyboardOptions {
|
|
enabled?: boolean;
|
|
}
|
|
|
|
export function useMobileKeyboard(
|
|
{ enabled = true }: UseMobileKeyboardOptions = {},
|
|
): { keyboardOverlap: number; viewportHeight: number | null; keyboardOpen: boolean } {
|
|
const [keyboardOverlap, setKeyboardOverlap] = useState(0);
|
|
const [viewportHeight, setViewportHeight] = useState<number | null>(null);
|
|
const [keyboardOpen, setKeyboardOpen] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!enabled || !isMobileDevice()) {
|
|
setKeyboardOverlap(0);
|
|
setViewportHeight(null);
|
|
setKeyboardOpen(false);
|
|
return;
|
|
}
|
|
|
|
const vv = window.visualViewport;
|
|
if (!vv) {
|
|
setKeyboardOverlap(0);
|
|
setViewportHeight(null);
|
|
setKeyboardOpen(false);
|
|
return;
|
|
}
|
|
|
|
const update = () => {
|
|
const metrics = getKeyboardMetrics();
|
|
setKeyboardOverlap(metrics.overlap);
|
|
setViewportHeight(metrics.vvHeight);
|
|
setKeyboardOpen(metrics.open);
|
|
};
|
|
|
|
update();
|
|
vv.addEventListener("resize", update);
|
|
vv.addEventListener("scroll", update);
|
|
document.addEventListener("focusin", update);
|
|
document.addEventListener("focusout", update);
|
|
|
|
return () => {
|
|
vv.removeEventListener("resize", update);
|
|
vv.removeEventListener("scroll", update);
|
|
document.removeEventListener("focusin", update);
|
|
document.removeEventListener("focusout", update);
|
|
setKeyboardOverlap(0);
|
|
setViewportHeight(null);
|
|
setKeyboardOpen(false);
|
|
};
|
|
}, [enabled]);
|
|
|
|
return { keyboardOverlap, viewportHeight, keyboardOpen };
|
|
}
|