Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor: collapse AutopilotPanel's per-option persist scaffold into a usePersistedOptions registry (#3675)

[._.]/ Adam Eivyfe3cad8de375
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index e535afe218..72294b1ef2 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -66,3 +66,5 @@
- A `/do:review` (or `/do:scan`, `/do:plan-task`, `/do:replan`) run queued from the **CoS quick templates** is no longer failed as `idle-no-changes`. The deliverable posture each slashdo workflow declares on its catalog entry reached the Agent Operations buttons but was silently dropped by the quick-template form, which read only the three run-shape toggles off a template's settings block — so the exact clean-tree misfire that posture exists to prevent still happened from the other surface. The flag now rides the template through the form as hidden state and into the create-task payload, and the strict template-settings schema accepts it so a user can save such a template back without a 400.
- **Series Pipeline runs can now pick a reasoning effort.** Pipeline stage LLM calls never emitted one, so a run's thinking effort was whatever the provider's configured CLI args (or an effort-encoding model id) happened to bake in — making a cheaper or harder-thinking autopilot run a global provider edit. Effort is now threaded exactly like the run provider and model: the Autonomous-mode Options popover (and a scheduled run's setup) offers it next to the provider/model picker for effort-capable providers, names the level the run resolves to, and applies it as a soft per-run default. A deliberate per-stage `effort` pin still wins, a level the chosen provider doesn't offer is clamped down its ladder rather than dropped, and a provider with no effort control simply gets no flag.
+
+- Internal: the Autonomous-mode Options popover's saved-default/per-run-override plumbing moved onto a shared `usePersistedOptions` hook. Each option was seven hand-copied edits scattered across a 1000-line component, and forgetting one silently produced the "I toggled it and ran, but it did not apply" bug; an option is now a single spec row that owns its default, its saved-value reader, its clamp and its persist behavior. No behavior change.
diff --git a/client/src/components/pipeline/AutopilotPanel.jsx b/client/src/components/pipeline/AutopilotPanel.jsx
index 125522897a..56f3c58508 100644
--- a/client/src/components/pipeline/AutopilotPanel.jsx
+++ b/client/src/components/pipeline/AutopilotPanel.jsx
@@ -6,6 +6,7 @@ import {
} from 'lucide-react';
import toast from '../ui/Toast';
import { usePipelineProgress } from '../../hooks/usePipelineProgress';
+import usePersistedOptions, { readBoolean, readInteger, readNumber } from '../../hooks/usePersistedOptions';
import {
startPipelineAutopilot,
cancelPipelineAutopilot,
@@ -96,14 +97,98 @@ const clampDelta = (n, fallback) => {
return Math.min(10, Math.max(0, v));
};
+// Every option that follows the "saved default + per-run override" contract, as
+// ONE row each: the display default, how to read a saved value (undefined =
+// absent/invalid → the default), the clamp applied both on blur and when the
+// value is sent as an override, and whether editing persists immediately.
+// `persistOnEdit` is for controls with no blur event (checkboxes); the numeric
+// inputs persist from RoundInput's onBlur instead. Keyed by the SETTING key the
+// server reads, so a key can't drift between the load, the save and the run.
+// Per-run-only options (readiness gate, unlock pre-pass, provider/model/effort)
+// are deliberately NOT here — they are never persisted.
+const OPTION_SPECS = {
+ maxArcVerifyRounds: {
+ defaultValue: DEFAULT_ARC_ROUNDS,
+ read: readInteger,
+ clamp: (v) => clampRound(v, DEFAULT_ARC_ROUNDS),
+ },
+ maxEditorialRounds: {
+ defaultValue: DEFAULT_EDITORIAL_ROUNDS,
+ read: readInteger,
+ clamp: (v) => clampRound(v, DEFAULT_EDITORIAL_ROUNDS),
+ },
+ maxBeatContinuityRounds: {
+ defaultValue: DEFAULT_BEAT_CONTINUITY_ROUNDS,
+ read: readInteger,
+ clamp: (v) => clampRound(v, DEFAULT_BEAT_CONTINUITY_ROUNDS),
+ },
+ // #1613 — non-negative integer, no upper cap (0 = off).
+ checkFindingsPauseThreshold: {
+ defaultValue: DEFAULT_CHECK_PAUSE_THRESHOLD,
+ read: readInteger,
+ clamp: clampThreshold,
+ },
+ // #1615 — plain boolean, no clamp. Defaults ON.
+ notifyOnPause: {
+ defaultValue: DEFAULT_NOTIFY_ON_PAUSE,
+ read: readBoolean,
+ persistOnEdit: true,
+ },
+ // #2171 — revision loop: enable checkbox + cycle bounds + plateau delta.
+ revisionEnabled: {
+ defaultValue: DEFAULT_REVISION_ENABLED,
+ read: readBoolean,
+ persistOnEdit: true,
+ },
+ revisionMinCycles: {
+ defaultValue: DEFAULT_REVISION_MIN_CYCLES,
+ read: readInteger,
+ clamp: (v) => clampCycles(v, DEFAULT_REVISION_MIN_CYCLES),
+ },
+ revisionMaxCycles: {
+ defaultValue: DEFAULT_REVISION_MAX_CYCLES,
+ read: readInteger,
+ clamp: (v) => clampCycles(v, DEFAULT_REVISION_MAX_CYCLES),
+ },
+ revisionPlateauDelta: {
+ defaultValue: DEFAULT_REVISION_PLATEAU_DELTA,
+ read: readNumber,
+ clamp: (v) => clampDelta(v, DEFAULT_REVISION_PLATEAU_DELTA),
+ },
+ // #2176 — foundation gate (defaults ON) + weighted threshold + round bound.
+ foundationGate: {
+ defaultValue: DEFAULT_FOUNDATION_GATE,
+ read: readBoolean,
+ persistOnEdit: true,
+ },
+ foundationThreshold: {
+ defaultValue: DEFAULT_FOUNDATION_THRESHOLD,
+ read: readNumber,
+ clamp: (v) => clampFoundationThreshold(v, DEFAULT_FOUNDATION_THRESHOLD),
+ },
+ maxFoundationRounds: {
+ defaultValue: DEFAULT_FOUNDATION_ROUNDS,
+ read: readInteger,
+ clamp: (v) => clampRound(v, DEFAULT_FOUNDATION_ROUNDS),
+ },
+ // Pipeline self-improvement — boolean, off by default.
+ selfImprove: {
+ defaultValue: DEFAULT_SELF_IMPROVE,
+ read: readBoolean,
+ persistOnEdit: true,
+ },
+};
+
// A single numeric field for the Options popover (round bounds + the pause
// threshold). Allows '' mid-edit (so the field can be cleared) and clamps +
// persists the chosen value on blur — but ONLY when the user actually changed it.
// A bare focus+blur (tabbing through Options) must not persist the display
// fallback or mark the field dirty, or it would clobber a saved limit before
// settings load and block the load from applying it. `max` caps the input (null =
-// uncapped); `clamp(value, defaultValue)` defaults to the round clamp.
-function RoundInput({ id, label, settingKey, value, setValue, defaultValue, persist, max = ROUND_MAX, clamp = clampRound, min = ROUND_MIN, step }) {
+// uncapped). `settingKey` / `value` / `setValue` / `clamp` / `persist` come from
+// the option registry's `inputProps(key)` — the clamp is the option's own, so
+// what a blur saves is exactly what a run sends.
+function RoundInput({ id, label, settingKey, value, setValue, persist, max = ROUND_MAX, clamp, min = ROUND_MIN, step }) {
const dirtyRef = useRef(false);
return (
<div className="flex items-center gap-2">
@@ -119,7 +204,7 @@ function RoundInput({ id, label, settingKey, value, setValue, defaultValue, pers
onBlur={() => {
if (!dirtyRef.current) return; // untouched — don't persist/clamp/mark dirty
dirtyRef.current = false;
- const v = clamp(value, defaultValue);
+ const v = clamp(value);
setValue(v);
persist({ [settingKey]: v });
}}
@@ -265,61 +350,37 @@ export default function AutopilotPanel({ series, onSeriesUpdate, onIssuesUpdate
const [showOpts, setShowOpts] = useState(false);
const [includeVisual, setIncludeVisual] = useState(true);
const [fileGaps, setFileGaps] = useState(false);
- const [arcRounds, setArcRounds] = useState(DEFAULT_ARC_ROUNDS);
- const [editorialRounds, setEditorialRounds] = useState(DEFAULT_EDITORIAL_ROUNDS);
- const [beatContinuityRounds, setBeatContinuityRounds] = useState(DEFAULT_BEAT_CONTINUITY_ROUNDS);
+ // Persist a setting (clamped) so a later Resume picks it up server-side.
+ // patchSettingsSlice is a GET-merge-PUT, so two overlapping calls (a blur save
+ // racing start()'s save) can lose an update — a slow earlier PUT lands after a
+ // newer one and clobbers it. Serialize every write onto one tail promise so the
+ // cycles can't interleave; start() awaiting its own enqueued write transitively
+ // awaits any in-flight blur save. Returns the promise so start() can await it.
+ const persistTailRef = useRef(Promise.resolve());
+ const persistRounds = useCallback((patch) => {
+ const next = persistTailRef.current
+ .catch(() => {})
+ .then(() => patchSettingsSlice('pipelineEditorialChecks', patch, { silent: true }).catch(() => null));
+ persistTailRef.current = next;
+ return next;
+ }, []);
+ // Every persisted option (saved default + per-run override) in one registry —
+ // see OPTION_SPECS. The registry owns the state, the per-field dirty flags, the
+ // hydrate-if-untouched pass and the edited-only override collection, so adding
+ // an option is one spec row plus its control, not seven scattered edits.
+ const options = usePersistedOptions(OPTION_SPECS, persistRounds);
+ const opt = options.values;
// Per-run readiness-gate override (#1580). '' = use the saved default (send
// nothing). `savedGate` is the persisted gate, shown in the "saved default"
// option label so the user knows what the fallback is.
const [readinessGate, setReadinessGate] = useState('');
const [savedGate, setSavedGate] = useState('');
- // Editorial-checks pause threshold (#1613). 0 = off (default): pause the run
- // when the editorial-checks pass surfaces ≥ N high findings. Persisted like the
- // round inputs (saved default + per-run override), so a raised value is reused
- // on Resume.
- const [checkPauseThreshold, setCheckPauseThreshold] = useState(DEFAULT_CHECK_PAUSE_THRESHOLD);
- // Pause-notification escalation (#1615). Persisted like the round inputs (saved
- // default + per-run override) but defaults ON — toggling off silences the
- // in-app pause banner. Persisted on change so the choice is reused on Resume.
- const [notifyOnPause, setNotifyOnPause] = useState(DEFAULT_NOTIFY_ON_PAUSE);
- // Iterate-to-quality revision loop (#2171). Persisted like the other options
- // (saved default + per-run override) so a configured loop is reused on Resume.
- const [revisionEnabled, setRevisionEnabled] = useState(DEFAULT_REVISION_ENABLED);
- const [revisionMinCycles, setRevisionMinCycles] = useState(DEFAULT_REVISION_MIN_CYCLES);
- const [revisionMaxCycles, setRevisionMaxCycles] = useState(DEFAULT_REVISION_MAX_CYCLES);
- const [revisionPlateauDelta, setRevisionPlateauDelta] = useState(DEFAULT_REVISION_PLATEAU_DELTA);
// Unlock-everything pre-pass. PER-RUN ONLY and never persisted (like
// `readinessGate` below, unlike every other option here): it rewrites lock
// state the user set by hand, and a saved default would be picked up by
// scheduled unattended runs of every series. Always starts unticked, so
// clearing locks is a fresh, deliberate choice each run.
const [unlockForRun, setUnlockForRun] = useState(false);
- // Foundation-quality gate (#2176) — enable toggle + weighted threshold + round
- // bound. Persisted like the other options (saved default + per-run override).
- const [foundationGate, setFoundationGate] = useState(DEFAULT_FOUNDATION_GATE);
- const [foundationThreshold, setFoundationThreshold] = useState(DEFAULT_FOUNDATION_THRESHOLD);
- const [foundationRounds, setFoundationRounds] = useState(DEFAULT_FOUNDATION_ROUNDS);
- // Pipeline self-improvement. Persisted like the other options (saved default +
- // per-run override) so an enabled diagnosis is reused on Resume.
- const [selfImprove, setSelfImprove] = useState(DEFAULT_SELF_IMPROVE);
- // Per-field dirty flags. Until a field is edited its input shows a display
- // default we must NOT persist (that would clobber a higher saved setting on
- // the untouched gate). Tracked per-field so editing one gate never discards
- // the loaded value of the other. start() persists ONLY the edited fields; the
- // untouched ones keep their on-disk value via patchSettingsSlice's merge.
- const arcEditedRef = useRef(false);
- const editorialEditedRef = useRef(false);
- const beatContinuityEditedRef = useRef(false);
- const checkPauseEditedRef = useRef(false);
- const notifyEditedRef = useRef(false);
- const revisionEnabledEditedRef = useRef(false);
- const revisionMinEditedRef = useRef(false);
- const revisionMaxEditedRef = useRef(false);
- const revisionDeltaEditedRef = useRef(false);
- const foundationGateEditedRef = useRef(false);
- const foundationThresholdEditedRef = useRef(false);
- const foundationRoundsEditedRef = useRef(false);
- const selfImproveEditedRef = useRef(false);
const [canon, setCanon] = useState(null);
const [canonLoading, setCanonLoading] = useState(false);
// Per-run provider/model override. '' = "use the series default", i.e. the
@@ -395,90 +456,26 @@ export default function AutopilotPanel({ series, onSeriesUpdate, onIssuesUpdate
// for its out-of-ladder option.
const effectiveEffort = resolveCliEffort(effortOverride, effProvider, effModel);
- // Load the persisted convergence-round defaults so the Options inputs reflect
- // the install's setting. The autopilot reads the same setting server-side, so
- // we never send these as per-run overrides — we just keep the UI in sync and
- // persist edits back. Apply the fetched value only to fields the user hasn't
- // already edited, so a slow load can't clobber a fast edit (per-field).
+ // Load the persisted option defaults so the Options controls reflect the
+ // install's settings. The autopilot reads the same settings server-side, so we
+ // never send an untouched option as a per-run override — we just keep the UI in
+ // sync and persist edits back. `hydrate` applies the fetched values only to
+ // fields the user hasn't already edited, so a slow load can't clobber a fast
+ // edit (per-field).
+ const { hydrate } = options;
useEffect(() => {
let canceled = false;
getSettings({ silent: true })
.then((s) => {
if (canceled) return;
const pec = s?.pipelineEditorialChecks || {};
- if (!arcEditedRef.current) setArcRounds(Number.isInteger(pec.maxArcVerifyRounds) ? pec.maxArcVerifyRounds : DEFAULT_ARC_ROUNDS);
- if (!editorialEditedRef.current) setEditorialRounds(Number.isInteger(pec.maxEditorialRounds) ? pec.maxEditorialRounds : DEFAULT_EDITORIAL_ROUNDS);
- if (!beatContinuityEditedRef.current) setBeatContinuityRounds(Number.isInteger(pec.maxBeatContinuityRounds) ? pec.maxBeatContinuityRounds : DEFAULT_BEAT_CONTINUITY_ROUNDS);
- if (!checkPauseEditedRef.current) setCheckPauseThreshold(Number.isInteger(pec.checkFindingsPauseThreshold) ? pec.checkFindingsPauseThreshold : DEFAULT_CHECK_PAUSE_THRESHOLD);
- if (!notifyEditedRef.current) setNotifyOnPause(typeof pec.notifyOnPause === 'boolean' ? pec.notifyOnPause : DEFAULT_NOTIFY_ON_PAUSE);
- if (!revisionEnabledEditedRef.current) setRevisionEnabled(typeof pec.revisionEnabled === 'boolean' ? pec.revisionEnabled : DEFAULT_REVISION_ENABLED);
- if (!revisionMinEditedRef.current) setRevisionMinCycles(Number.isInteger(pec.revisionMinCycles) ? pec.revisionMinCycles : DEFAULT_REVISION_MIN_CYCLES);
- if (!revisionMaxEditedRef.current) setRevisionMaxCycles(Number.isInteger(pec.revisionMaxCycles) ? pec.revisionMaxCycles : DEFAULT_REVISION_MAX_CYCLES);
- if (!revisionDeltaEditedRef.current) setRevisionPlateauDelta(Number.isFinite(pec.revisionPlateauDelta) ? pec.revisionPlateauDelta : DEFAULT_REVISION_PLATEAU_DELTA);
- if (!foundationGateEditedRef.current) setFoundationGate(typeof pec.foundationGate === 'boolean' ? pec.foundationGate : DEFAULT_FOUNDATION_GATE);
- if (!foundationThresholdEditedRef.current) setFoundationThreshold(Number.isFinite(pec.foundationThreshold) ? pec.foundationThreshold : DEFAULT_FOUNDATION_THRESHOLD);
- if (!foundationRoundsEditedRef.current) setFoundationRounds(Number.isInteger(pec.maxFoundationRounds) ? pec.maxFoundationRounds : DEFAULT_FOUNDATION_ROUNDS);
- if (!selfImproveEditedRef.current) setSelfImprove(typeof pec.selfImprove === 'boolean' ? pec.selfImprove : DEFAULT_SELF_IMPROVE);
+ hydrate(pec);
// Persisted readiness gate — display-only, drives the "saved default" label.
setSavedGate(READINESS_GATE_LABELS[pec.readinessGate] ? pec.readinessGate : '');
})
.catch(() => null); // load failed → inputs keep defaults but start() only persists EDITED fields
return () => { canceled = true; };
- }, []);
-
- // Persist a round setting (clamped) so a later Resume picks it up server-side.
- // patchSettingsSlice is a GET-merge-PUT, so two overlapping calls (a blur save
- // racing start()'s save) can lose an update — a slow earlier PUT lands after a
- // newer one and clobbers it. Serialize every write onto one tail promise so the
- // cycles can't interleave; start() awaiting its own enqueued write transitively
- // awaits any in-flight blur save. Returns the promise so start() can await it.
- const persistTailRef = useRef(Promise.resolve());
- const persistRounds = useCallback((patch) => {
- const next = persistTailRef.current
- .catch(() => {})
- .then(() => patchSettingsSlice('pipelineEditorialChecks', patch, { silent: true }).catch(() => null));
- persistTailRef.current = next;
- return next;
- }, []);
-
- // User edited an input — mark that field dirty so a late settings load can't
- // overwrite it and so start() knows to persist it.
- const editArcRounds = useCallback((v) => { arcEditedRef.current = true; setArcRounds(v); }, []);
- const editEditorialRounds = useCallback((v) => { editorialEditedRef.current = true; setEditorialRounds(v); }, []);
- const editBeatContinuityRounds = useCallback((v) => { beatContinuityEditedRef.current = true; setBeatContinuityRounds(v); }, []);
- const editCheckPauseThreshold = useCallback((v) => { checkPauseEditedRef.current = true; setCheckPauseThreshold(v); }, []);
- const editFoundationThreshold = useCallback((v) => { foundationThresholdEditedRef.current = true; setFoundationThreshold(v); }, []);
- const editFoundationRounds = useCallback((v) => { foundationRoundsEditedRef.current = true; setFoundationRounds(v); }, []);
- // Boolean toggle — persist immediately (like notifyOnPause) so the saved
- // default tracks the choice and Resume reuses it.
- const editFoundationGate = useCallback((v) => {
- foundationGateEditedRef.current = true;
- setFoundationGate(v);
- persistRounds({ foundationGate: v });
- }, [persistRounds]);
- // Boolean checkbox — persist immediately on toggle (no blur event) so the saved
- // default tracks the choice and Resume reuses it.
- const editNotifyOnPause = useCallback((v) => {
- notifyEditedRef.current = true;
- setNotifyOnPause(v);
- persistRounds({ notifyOnPause: v });
- }, [persistRounds]);
- // Revision loop (#2171): the enable checkbox persists immediately (no blur); the
- // cycle bounds + plateau delta persist on blur via RoundInput like the others.
- const editRevisionEnabled = useCallback((v) => {
- revisionEnabledEditedRef.current = true;
- setRevisionEnabled(v);
- persistRounds({ revisionEnabled: v });
- }, [persistRounds]);
- // Boolean checkbox — persists immediately on toggle (no blur) like notifyOnPause.
- const editSelfImprove = useCallback((v) => {
- selfImproveEditedRef.current = true;
- setSelfImprove(v);
- persistRounds({ selfImprove: v });
- }, [persistRounds]);
- const editRevisionMinCycles = useCallback((v) => { revisionMinEditedRef.current = true; setRevisionMinCycles(v); }, []);
- const editRevisionMaxCycles = useCallback((v) => { revisionMaxEditedRef.current = true; setRevisionMaxCycles(v); }, []);
- const editRevisionPlateauDelta = useCallback((v) => { revisionDeltaEditedRef.current = true; setRevisionPlateauDelta(v); }, []);
+ }, [hydrate]);
const { latest, frames } = usePipelineProgress(pipelineAutopilotSseUrl, [seriesId], { enabled: active });
@@ -558,36 +555,14 @@ export default function AutopilotPanel({ series, onSeriesUpdate, onIssuesUpdate
setStarting(true);
setPlan(null);
setPlanTotals(null);
- // Collect ONLY the gates the user edited (clamped, real values — never the
- // display defaults of untouched gates, which would mask a saved setting). Send
- // them as per-run overrides AND persist them: the override makes the edit
- // effective for THIS run even if the save fails (persist is best-effort,
- // server precedence is per-run → setting → default), and the persist makes it
- // the saved default for next time. Untouched gates send nothing, so the server
+ // ONLY the options the user edited (clamped, real values — never the display
+ // defaults of untouched options, which would mask a saved setting). Send them
+ // as per-run overrides AND persist them: the override makes the edit effective
+ // for THIS run even if the save fails (persist is best-effort, server
+ // precedence is per-run → setting → default), and the persist makes it the
+ // saved default for next time. Untouched options send nothing, so the server
// resolves them from the persisted setting.
- const roundOverrides = {};
- if (arcEditedRef.current) roundOverrides.maxArcVerifyRounds = clampRound(arcRounds, DEFAULT_ARC_ROUNDS);
- if (editorialEditedRef.current) roundOverrides.maxEditorialRounds = clampRound(editorialRounds, DEFAULT_EDITORIAL_ROUNDS);
- if (beatContinuityEditedRef.current) roundOverrides.maxBeatContinuityRounds = clampRound(beatContinuityRounds, DEFAULT_BEAT_CONTINUITY_ROUNDS);
- // #1613 — the editorial-checks pause threshold persists + overrides like the
- // round inputs (clamps to a non-negative integer; 0 = off).
- if (checkPauseEditedRef.current) roundOverrides.checkFindingsPauseThreshold = clampThreshold(checkPauseThreshold);
- // #1615 — pause notification toggle persists + overrides like the others, but
- // is a plain boolean (no clamp). Untouched sends nothing → server default (on).
- if (notifyEditedRef.current) roundOverrides.notifyOnPause = notifyOnPause;
- // #2171 — the revision-loop knobs persist + override like the round inputs.
- if (revisionEnabledEditedRef.current) roundOverrides.revisionEnabled = revisionEnabled;
- if (revisionMinEditedRef.current) roundOverrides.revisionMinCycles = clampCycles(revisionMinCycles, DEFAULT_REVISION_MIN_CYCLES);
- if (revisionMaxEditedRef.current) roundOverrides.revisionMaxCycles = clampCycles(revisionMaxCycles, DEFAULT_REVISION_MAX_CYCLES);
- if (revisionDeltaEditedRef.current) roundOverrides.revisionPlateauDelta = clampDelta(revisionPlateauDelta, DEFAULT_REVISION_PLATEAU_DELTA);
- // #2176 — foundation gate. Persist + override like the other options, only
- // when edited (untouched → server resolves from the persisted setting, then
- // the default: gate ON / threshold 7.5 / 3 rounds).
- if (foundationGateEditedRef.current) roundOverrides.foundationGate = foundationGate;
- if (foundationThresholdEditedRef.current) roundOverrides.foundationThreshold = clampFoundationThreshold(foundationThreshold, DEFAULT_FOUNDATION_THRESHOLD);
- if (foundationRoundsEditedRef.current) roundOverrides.maxFoundationRounds = clampRound(foundationRounds, DEFAULT_FOUNDATION_ROUNDS);
- // Self-improvement persists + overrides like the other booleans.
- if (selfImproveEditedRef.current) roundOverrides.selfImprove = selfImprove;
+ const roundOverrides = options.collectOverrides();
if (Object.keys(roundOverrides).length) await persistRounds(roundOverrides);
// Per-run readiness-gate override (#1580): send it ONLY when the user picked a
// specific gate. Unlike the round inputs we never persist it — '' leaves the
@@ -622,7 +597,9 @@ export default function AutopilotPanel({ series, onSeriesUpdate, onIssuesUpdate
// effect can reject a stale terminal frame from the previous run.
activeRunIdRef.current = res.runId || null;
setActive(true);
- }, [seriesId, includeVisual, fileGaps, arcRounds, editorialRounds, beatContinuityRounds, checkPauseThreshold, notifyOnPause, unlockForRun, revisionEnabled, revisionMinCycles, revisionMaxCycles, revisionPlateauDelta, foundationGate, foundationThreshold, foundationRounds, selfImprove, readinessGate, providerOverride, modelOverride, effortOverride, persistRounds]);
+ // `options` is the single dep for every persisted option — the registry reads
+ // live values through refs, so no option can be forgotten here.
+ }, [seriesId, includeVisual, fileGaps, unlockForRun, readinessGate, providerOverride, modelOverride, effortOverride, options, persistRounds]);
const cancel = useCallback(async () => {
await cancelPipelineAutopilot(seriesId).catch(() => null);
@@ -744,11 +721,11 @@ export default function AutopilotPanel({ series, onSeriesUpdate, onIssuesUpdate
File CoS tasks for gaps it can&apos;t resolve
</label>
<label className="flex items-center gap-2 text-xs text-gray-300">
- <input type="checkbox" checked={notifyOnPause} onChange={(e) => editNotifyOnPause(e.target.checked)} />
+ <input type="checkbox" checked={opt.notifyOnPause} onChange={(e) => options.edit('notifyOnPause', e.target.checked)} />
Notify me when a run pauses (with a resume link)
</label>
<label className="flex items-center gap-2 text-xs text-gray-300">
- <input type="checkbox" checked={foundationGate} onChange={(e) => editFoundationGate(e.target.checked)} />
+ <input type="checkbox" checked={opt.foundationGate} onChange={(e) => options.edit('foundationGate', e.target.checked)} />
Judge the foundation (world / characters / arc) before drafting
</label>
<label className="flex items-center gap-2 text-xs text-gray-300">
@@ -764,56 +741,35 @@ export default function AutopilotPanel({ series, onSeriesUpdate, onIssuesUpdate
<RoundInput
id="autopilot-arc-rounds"
label="Arc verify rounds"
- settingKey="maxArcVerifyRounds"
- value={arcRounds}
- setValue={editArcRounds}
- defaultValue={DEFAULT_ARC_ROUNDS}
- persist={persistRounds}
+ {...options.inputProps('maxArcVerifyRounds')}
/>
<RoundInput
id="autopilot-beat-continuity-rounds"
label="Beat continuity rounds"
- settingKey="maxBeatContinuityRounds"
- value={beatContinuityRounds}
- setValue={editBeatContinuityRounds}
- defaultValue={DEFAULT_BEAT_CONTINUITY_ROUNDS}
- persist={persistRounds}
+ {...options.inputProps('maxBeatContinuityRounds')}
/>
<RoundInput
id="autopilot-editorial-rounds"
label="Editorial rounds"
- settingKey="maxEditorialRounds"
- value={editorialRounds}
- setValue={editEditorialRounds}
- defaultValue={DEFAULT_EDITORIAL_ROUNDS}
- persist={persistRounds}
+ {...options.inputProps('maxEditorialRounds')}
/>
</div>
<p className="text-[11px] text-gray-500">
How many auto-resolve rounds each gate attempts before pausing for human review (0 skips the gate, max {ROUND_MAX}). Saved as the default and reused on Resume.
</p>
- {foundationGate ? (
+ {opt.foundationGate ? (
<>
<div className="flex flex-wrap gap-4 pt-1">
<RoundInput
id="autopilot-foundation-threshold"
label="Foundation threshold"
- settingKey="foundationThreshold"
- value={foundationThreshold}
- setValue={editFoundationThreshold}
- defaultValue={DEFAULT_FOUNDATION_THRESHOLD}
- persist={persistRounds}
+ {...options.inputProps('foundationThreshold')}
max={10}
- clamp={clampFoundationThreshold}
/>
<RoundInput
id="autopilot-foundation-rounds"
label="Foundation rounds"
- settingKey="maxFoundationRounds"
- value={foundationRounds}
- setValue={editFoundationRounds}
- defaultValue={DEFAULT_FOUNDATION_ROUNDS}
- persist={persistRounds}
+ {...options.inputProps('maxFoundationRounds')}
/>
</div>
<p className="text-[11px] text-gray-500">
@@ -844,59 +800,39 @@ export default function AutopilotPanel({ series, onSeriesUpdate, onIssuesUpdate
<RoundInput
id="autopilot-check-pause-threshold"
label="Pause at high findings"
- settingKey="checkFindingsPauseThreshold"
- value={checkPauseThreshold}
- setValue={editCheckPauseThreshold}
- defaultValue={DEFAULT_CHECK_PAUSE_THRESHOLD}
- persist={persistRounds}
+ {...options.inputProps('checkFindingsPauseThreshold')}
max={null}
- clamp={clampThreshold}
/>
</div>
<p className="text-[11px] text-gray-500">
When the editorial-checks pass surfaces this many High findings (or more), the run pauses for review instead of proceeding. 0 = off. Saved as the default and reused on Resume.
</p>
<label className="flex items-center gap-2 text-xs text-gray-300 pt-1 border-t border-port-border mt-1">
- <input type="checkbox" checked={revisionEnabled} onChange={(e) => editRevisionEnabled(e.target.checked)} />
+ <input type="checkbox" checked={opt.revisionEnabled} onChange={(e) => options.edit('revisionEnabled', e.target.checked)} />
Iterate to quality (revise the weakest issue under a keep/revert score gate)
</label>
- {revisionEnabled ? (
+ {opt.revisionEnabled ? (
<>
<div className="flex flex-wrap gap-4 pt-1">
<RoundInput
id="autopilot-revision-min-cycles"
label="Min cycles"
- settingKey="revisionMinCycles"
- value={revisionMinCycles}
- setValue={editRevisionMinCycles}
- defaultValue={DEFAULT_REVISION_MIN_CYCLES}
- persist={persistRounds}
+ {...options.inputProps('revisionMinCycles')}
min={1}
- clamp={clampCycles}
/>
<RoundInput
id="autopilot-revision-max-cycles"
label="Max cycles"
- settingKey="revisionMaxCycles"
- value={revisionMaxCycles}
- setValue={editRevisionMaxCycles}
- defaultValue={DEFAULT_REVISION_MAX_CYCLES}
- persist={persistRounds}
+ {...options.inputProps('revisionMaxCycles')}
min={1}
- clamp={clampCycles}
/>
<RoundInput
id="autopilot-revision-plateau-delta"
label="Plateau Δ"
- settingKey="revisionPlateauDelta"
- value={revisionPlateauDelta}
- setValue={editRevisionPlateauDelta}
- defaultValue={DEFAULT_REVISION_PLATEAU_DELTA}
- persist={persistRounds}
+ {...options.inputProps('revisionPlateauDelta')}
min={0}
max={10}
step={0.1}
- clamp={clampDelta}
/>
</div>
<p className="text-[11px] text-gray-500">
@@ -905,10 +841,10 @@ export default function AutopilotPanel({ series, onSeriesUpdate, onIssuesUpdate
</>
) : null}
<label className="flex items-center gap-2 text-xs text-gray-300 pt-1 border-t border-port-border mt-1">
- <input type="checkbox" checked={selfImprove} onChange={(e) => editSelfImprove(e.target.checked)} />
+ <input type="checkbox" checked={opt.selfImprove} onChange={(e) => options.edit('selfImprove', e.target.checked)} />
Improve the pipeline itself (diagnose PortOS when a run goes wrong)
</label>
- {selfImprove ? (
+ {opt.selfImprove ? (
<p className="text-[11px] text-gray-500">
When a run pauses, errors, or finishes with an editorial check that threw or a step that had to be retried, it spends one call asking whether the fault is the <em>story</em> or the <em>pipeline</em> — a missing editorial step earlier in the process, a stage prompt breaking its contract, a runner swallowing a failure. A pipeline verdict files a CoS task against PortOS itself: worktree-isolated, PR-opening, and waiting in your CoS approval queue — it never starts on its own. A healthy run never spends anything here. Saved as the default and reused on Resume.
</p>
diff --git a/client/src/hooks/README.md b/client/src/hooks/README.md
index 1fc4c2b0f9..c9dc6c85ba 100644
--- a/client/src/hooks/README.md
+++ b/client/src/hooks/README.md
@@ -109,6 +109,7 @@ grep -i "what you want to do" client/src/hooks/README.md
| `useKeyboardHelp` | Esc closes, even from inputs/textareas. | Help/cheatsheet modals. |
| `useLiveSuggest` | Post-typing debounce for an imperative panel trigger: `{ registerTrigger, scheduleSuggest }` re-arms one `debounceMs` timer per keystroke and fires the registered fn only while `enabled` and still mounted. | An editor that asks a sibling AI panel to suggest once the writer pauses (WorkEditor ↔ LiveContinuationPanel). |
| `useLockToggle` | Optimistic-PATCH lock toggle. | New "lock this field/stage/arc" button — use this, do not re-implement. |
+| `usePersistedOptions` | Registry for "saved default + per-run override" form options: one spec row per option (`{ defaultValue, read, clamp, persistOnEdit }`) owns the state, per-field dirty flag, hydrate-if-untouched pass and edited-only override collection. Returns `{ values, edit, hydrate, collectOverrides, inputProps }`, all identity-stable and ref-backed so a caller's `useCallback` needs one dep instead of one per option. | Any panel where each option must load a saved default, persist edits, and send ONLY edited fields as per-run overrides (AutopilotPanel) — use this instead of hand-copying the state/editedRef/hydrate/collect scaffold per option. |
| `usePopoverPosition` | Viewport-clamped `{ left, top, width }` for a fixed-position portal popover anchored to a trigger; re-measures on open and rAF-coalesced on capture-phase scroll/resize. Returns `{ triggerRef, popoverRef, style, reposition }`; pass `anchorRef` to follow a parent-owned trigger. | Any portal-into-`<body>` menu/popover placed relative to a button (ThemeSwitcher, CollectionPickerShell) — use this instead of re-rolling the measure/flip/clamp/reflow plumbing. |
| `useScrollLock` | Body-scroll lock with ref-count. | Modals, drawers, lightboxes. |
| `useSidebarResize` | Drag-to-resize split-pane width persisted to `localStorage`; returns `{ containerRef, width, onMouseDown, reset }` and caps the drag at `maxFraction` of the measured container. | A desktop sidebar/split pane with a draggable divider (WorkEditor's storyboard sidebar). |
diff --git a/client/src/hooks/index.js b/client/src/hooks/index.js
index bc3991ddbf..d68140b80e 100644
--- a/client/src/hooks/index.js
+++ b/client/src/hooks/index.js
@@ -147,6 +147,8 @@ export { default as useDrumPlayer } from './useDrumPlayer.js';
export * from './useHfTokenStatus.js';
export * from './useKeyboardHelp.js';
export * from './useLockToggle.js';
+export { default as usePersistedOptions } from './usePersistedOptions.js';
+export * from './usePersistedOptions.js';
export * from './useScrollLock.js';
export * from './useSwipeNav.js';
export { default as useUrlParams } from './useUrlParams.js';
diff --git a/client/src/hooks/usePersistedOptions.js b/client/src/hooks/usePersistedOptions.js
new file mode 100644
index 0000000000..380dc0c138
--- /dev/null
+++ b/client/src/hooks/usePersistedOptions.js
@@ -0,0 +1,110 @@
+import { useCallback, useMemo, useRef, useState } from 'react';
+
+/**
+ * Registry for "saved default + per-run override" options — the scaffold every
+ * autopilot-style option used to hand-copy seven times (a default const, a
+ * `useState`, an `editedRef`, an `edit*` callback, a hydrate-if-untouched line,
+ * an override-collect line, and a `start()` dep-array entry). Missing any one of
+ * those is a silent bug: an untouched field clobbers a higher saved setting, or
+ * an edited one never reaches the run. Here a whole option is ONE spec entry.
+ *
+ * The contract each spec buys you:
+ * - **Untouched fields never persist.** `hydrate()` only writes fields the user
+ * hasn't edited, so a slow settings load can't clobber a fast edit and an
+ * unedited display default never overwrites a saved value.
+ * - **Only edited fields become overrides.** `collectOverrides()` emits exactly
+ * the edited keys, clamped through the SAME `clamp` the input uses on blur —
+ * so the value sent can't drift from the value shown.
+ * - **No stale closures.** `edit` / `hydrate` / `collectOverrides` / `inputProps`
+ * are identity-stable and read live state through refs, so a caller's
+ * `useCallback` needs one dep (the registry) instead of one per option.
+ *
+ * @param {Record<string, {
+ * defaultValue: any,
+ * read?: (raw: any) => any, // undefined = "not a valid saved value" → defaultValue
+ * clamp?: (v: any) => any, // applied on blur-persist and override-collect
+ * persistOnEdit?: boolean, // booleans/selects with no blur event
+ * }>} specs Keyed by the SETTING key sent to the server. Treated as constant —
+ * define it at module scope (later renders keep the first spec object).
+ * @param {(patch: Record<string, any>) => any} [persist] Called with `{ key: value }`
+ * when a `persistOnEdit` option changes, and handed to `inputProps().persist`.
+ * Read through a ref, so passing a fresh closure each render is fine.
+ */
+export default function usePersistedOptions(specs, persist) {
+ // Specs are a constant table; pinning them keeps every returned callback
+ // identity-stable (the whole point — a caller's dep array stays 1 entry).
+ const specsRef = useRef(specs);
+ const persistRef = useRef(persist);
+ persistRef.current = persist;
+
+ const [values, setValues] = useState(() => Object.fromEntries(
+ Object.entries(specsRef.current).map(([key, spec]) => [key, spec.defaultValue]),
+ ));
+ // Live mirror so `collectOverrides` (called from an async start handler) never
+ // reads a stale closure of `values`.
+ const valuesRef = useRef(values);
+ valuesRef.current = values;
+ // Per-field dirty flags. Until a field is edited its input shows a display
+ // default we must NOT persist or send.
+ const editedRef = useRef({});
+
+ const edit = useCallback((key, value) => {
+ editedRef.current[key] = true;
+ setValues((prev) => ({ ...prev, [key]: value }));
+ // Persist OUTSIDE the state updater — an updater can run twice under
+ // StrictMode and would double-fire the PATCH.
+ if (specsRef.current[key]?.persistOnEdit) persistRef.current?.({ [key]: value });
+ }, []);
+
+ /** Apply loaded settings to every field the user hasn't edited. */
+ const hydrate = useCallback((saved) => {
+ const source = saved || {};
+ setValues((prev) => {
+ const next = { ...prev };
+ let changed = false;
+ for (const [key, spec] of Object.entries(specsRef.current)) {
+ if (editedRef.current[key]) continue;
+ const read = spec.read ? spec.read(source[key]) : source[key];
+ // `undefined` means absent-or-invalid — fall back to the default rather
+ // than letting a missing setting read as an intentional clear.
+ const value = read === undefined ? spec.defaultValue : read;
+ if (next[key] !== value) { changed = true; next[key] = value; }
+ }
+ return changed ? next : prev;
+ });
+ }, []);
+
+ /** `{ settingKey: clampedValue }` for the EDITED fields only. */
+ const collectOverrides = useCallback(() => {
+ const out = {};
+ for (const [key, spec] of Object.entries(specsRef.current)) {
+ if (!editedRef.current[key]) continue;
+ const value = valuesRef.current[key];
+ out[key] = spec.clamp ? spec.clamp(value) : value;
+ }
+ return out;
+ }, []);
+
+ /**
+ * Props for a controlled numeric field: value + a setter that marks the field
+ * edited, plus the spec's own clamp and the persist callback — so the value an
+ * input clamps on blur can't drift from the one `collectOverrides` sends.
+ */
+ const inputProps = useCallback((key) => ({
+ settingKey: key,
+ value: valuesRef.current[key],
+ setValue: (v) => edit(key, v),
+ clamp: specsRef.current[key].clamp,
+ persist: (patch) => persistRef.current?.(patch),
+ }), [edit]);
+
+ return useMemo(
+ () => ({ values, edit, hydrate, collectOverrides, inputProps }),
+ [values, edit, hydrate, collectOverrides, inputProps],
+ );
+}
+
+/** Saved-value readers. Each returns `undefined` for "absent or not this type". */
+export const readInteger = (raw) => (Number.isInteger(raw) ? raw : undefined);
+export const readNumber = (raw) => (Number.isFinite(raw) ? raw : undefined);
+export const readBoolean = (raw) => (typeof raw === 'boolean' ? raw : undefined);
diff --git a/client/src/hooks/usePersistedOptions.test.jsx b/client/src/hooks/usePersistedOptions.test.jsx
new file mode 100644
index 0000000000..0a09d34f68
--- /dev/null
+++ b/client/src/hooks/usePersistedOptions.test.jsx
@@ -0,0 +1,107 @@
+import { describe, it, expect, vi } from 'vitest';
+import { renderHook, act } from '@testing-library/react';
+import usePersistedOptions, { readBoolean, readInteger, readNumber } from './usePersistedOptions';
+
+const SPECS = {
+ rounds: { defaultValue: 3, read: readInteger, clamp: (v) => (v === '' ? 3 : Math.min(20, Math.max(0, Math.round(v)))) },
+ delta: { defaultValue: 0.3, read: readNumber, clamp: (v) => (v === '' ? 0.3 : v) },
+ enabled: { defaultValue: false, read: readBoolean, persistOnEdit: true },
+};
+
+const setup = (persist) => renderHook(({ p }) => usePersistedOptions(SPECS, p), {
+ initialProps: { p: persist },
+});
+
+describe('usePersistedOptions', () => {
+ it('starts every option at its declared default', () => {
+ const { result } = setup();
+ expect(result.current.values).toEqual({ rounds: 3, delta: 0.3, enabled: false });
+ expect(result.current.collectOverrides()).toEqual({});
+ });
+
+ it('hydrates untouched options from saved settings', () => {
+ const { result } = setup();
+ act(() => result.current.hydrate({ rounds: 7, delta: 1.5, enabled: true }));
+ expect(result.current.values).toEqual({ rounds: 7, delta: 1.5, enabled: true });
+ // Hydration is not an edit — nothing is sent as a per-run override.
+ expect(result.current.collectOverrides()).toEqual({});
+ });
+
+ it('falls back to the default for an absent or wrong-typed saved value', () => {
+ const { result } = setup();
+ act(() => result.current.hydrate({ rounds: 'nine', enabled: 'yes' }));
+ expect(result.current.values).toEqual({ rounds: 3, delta: 0.3, enabled: false });
+ });
+
+ it('never lets a late load clobber an edited field, per field', () => {
+ const { result } = setup();
+ act(() => result.current.edit('rounds', 9));
+ act(() => result.current.hydrate({ rounds: 1, delta: 2 }));
+ expect(result.current.values.rounds).toBe(9); // edited — load ignored
+ expect(result.current.values.delta).toBe(2); // untouched — load applied
+ });
+
+ it('collects ONLY edited options, clamped', () => {
+ const { result } = setup();
+ act(() => result.current.edit('rounds', 99));
+ expect(result.current.collectOverrides()).toEqual({ rounds: 20 });
+ });
+
+ it('sends an explicitly edited falsy value rather than dropping it', () => {
+ const { result } = setup();
+ act(() => result.current.hydrate({ rounds: 5, enabled: true }));
+ act(() => result.current.edit('rounds', 0));
+ act(() => result.current.edit('enabled', false));
+ expect(result.current.collectOverrides()).toEqual({ rounds: 0, enabled: false });
+ });
+
+ it('persists a persistOnEdit option immediately, exactly once', () => {
+ const persist = vi.fn();
+ const { result } = setup(persist);
+ act(() => result.current.edit('enabled', true));
+ expect(persist).toHaveBeenCalledTimes(1);
+ expect(persist).toHaveBeenCalledWith({ enabled: true });
+ });
+
+ it('does not persist an option that saves on blur instead', () => {
+ const persist = vi.fn();
+ const { result } = setup(persist);
+ act(() => result.current.edit('rounds', 4));
+ expect(persist).not.toHaveBeenCalled();
+ });
+
+ it('calls the LATEST persist callback, not the one captured at mount', () => {
+ const first = vi.fn();
+ const second = vi.fn();
+ const { result, rerender } = setup(first);
+ rerender({ p: second });
+ act(() => result.current.edit('enabled', true));
+ expect(first).not.toHaveBeenCalled();
+ expect(second).toHaveBeenCalledWith({ enabled: true });
+ });
+
+ it('keeps every callback identity-stable across renders and edits', () => {
+ const { result } = setup();
+ const before = result.current;
+ act(() => result.current.edit('rounds', 5));
+ expect(result.current.edit).toBe(before.edit);
+ expect(result.current.hydrate).toBe(before.hydrate);
+ expect(result.current.collectOverrides).toBe(before.collectOverrides);
+ expect(result.current.inputProps).toBe(before.inputProps);
+ });
+
+ it('inputProps binds the option key, live value, its clamp and persist', () => {
+ const persist = vi.fn();
+ const { result } = setup(persist);
+ act(() => result.current.hydrate({ rounds: 6 }));
+ const props = result.current.inputProps('rounds');
+ expect(props.settingKey).toBe('rounds');
+ expect(props.value).toBe(6);
+ expect(props.clamp(99)).toBe(20); // same clamp collectOverrides applies
+ act(() => props.setValue(8));
+ expect(result.current.values.rounds).toBe(8);
+ expect(result.current.collectOverrides()).toEqual({ rounds: 8 });
+ props.persist({ rounds: 8 });
+ expect(persist).toHaveBeenCalledWith({ rounds: 8 });
+ });
+});