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

fix: keep the job-skill discard prompt in sync with dirty state and in-flight saves (#3939)

[._.]/ Adam Eivy4cc1b892cc07
diff --git a/client/src/pages/PromptManager.jsx b/client/src/pages/PromptManager.jsx
index 7d24cb5aa7..013cf51512 100644
--- a/client/src/pages/PromptManager.jsx
+++ b/client/src/pages/PromptManager.jsx
@@ -110,6 +110,11 @@ export default function PromptManager() {
const [jobSkillMeta, setJobSkillMeta] = useState({});
const [jobSkillPreview, setJobSkillPreview] = useState('');
const isJobSkillDirty = Boolean(selectedJobSkill) && jobSkillContent !== savedJobSkillContent;
+ // `saveJobSkill` resumes after an await holding the values its closure captured
+ // at click time, so it reads the live selection/text through these refs to tell
+ // "still the same editor" from "the user moved on mid-flight".
+ const jobSkillLiveRef = useRef({ selected: selectedJobSkill, content: jobSkillContent });
+ jobSkillLiveRef.current = { selected: selectedJobSkill, content: jobSkillContent };
const [providers, setProviders] = useState([]);
const [activeProviderId, setActiveProviderId] = useState(null);
@@ -330,13 +335,23 @@ export default function PromptManager() {
setSaving(true);
// Snapshot what we actually sent — the textarea may change while the PATCH
// is in flight, and only the persisted text may become the clean baseline.
+ // The skill is snapshotted too: if the editor has moved on by the time the
+ // PUT resolves, `sent` belongs to the PREVIOUS skill and adopting it as the
+ // baseline would flag the freshly loaded one as dirty.
const sent = jobSkillContent;
- const ok = await apiSaveJobSkill(selectedJobSkill, sent, { silent: true })
+ const sentFor = selectedJobSkill;
+ const ok = await apiSaveJobSkill(sentFor, sent, { silent: true })
.then(() => true)
.catch((err) => { toast.error(`Failed to save job skill: ${err.message || 'Unknown error'}`); return false; });
setSaving(false);
if (!ok) return;
- setSavedJobSkillContent(sent);
+ const live = jobSkillLiveRef.current;
+ if (sentFor === live.selected) {
+ setSavedJobSkillContent(sent);
+ // Saving answers the pending discard prompt — there is nothing left to
+ // lose, unless the user kept typing while the PUT was open.
+ if (sent === live.content) setPendingJobSkill(null);
+ }
toast.success('Job skill saved');
};
@@ -344,7 +359,12 @@ export default function PromptManager() {
// confirmed away first (#3939). The clicked skill parks in `pendingJobSkill`
// and an inline confirm row takes over its list slot — no window.confirm.
const requestJobSkill = (name) => {
- if (name === selectedJobSkill) return;
+ if (name === selectedJobSkill) {
+ // Re-clicking the open skill is how a user backs out of the prompt from
+ // the list side; leaving it armed would strand the row mid-question.
+ setPendingJobSkill(null);
+ return;
+ }
if (isJobSkillDirty) {
setPendingJobSkill(name);
return;
@@ -866,7 +886,10 @@ export default function PromptManager() {
</div>
<div className="space-y-1">
{jobSkills.map((skill) => (
- pendingJobSkill === skill.name ? (
+ // The dirty check is part of the render condition, not just of
+ // arming: undoing the edit back to the saved text while the row
+ // is armed leaves nothing to discard, so the question must go.
+ (pendingJobSkill === skill.name && isJobSkillDirty) ? (
<InlineConfirmRow
key={skill.name}
className="rounded-lg"
diff --git a/client/src/pages/PromptManager.test.jsx b/client/src/pages/PromptManager.test.jsx
index 615d18cff7..c0bc542a4c 100644
--- a/client/src/pages/PromptManager.test.jsx
+++ b/client/src/pages/PromptManager.test.jsx
@@ -674,6 +674,55 @@ describe('PromptManager job skill unsaved-edit guard', () => {
await waitFor(() => expect(currentSearch()).toContain('skill=doc-writer'));
});
+ it('drops the armed prompt when the edit is undone back to the saved text', async () => {
+ await openDirtyEditor();
+ fireEvent.click(screen.getByText('doc-writer'));
+ expect(screen.getByText(/Discard unsaved changes/)).toBeTruthy();
+
+ fireEvent.change(editor(), { target: { value: '# code-fixer template' } });
+
+ expect(screen.queryByText(/Discard unsaved changes/)).toBeNull();
+ expect(screen.getByText('doc-writer')).toBeTruthy();
+ });
+
+ it('drops the armed prompt when the edits are saved instead of discarded', async () => {
+ await openDirtyEditor();
+ fireEvent.click(screen.getByText('doc-writer'));
+ expect(screen.getByText(/Discard unsaved changes/)).toBeTruthy();
+
+ fireEvent.click(screen.getByRole('button', { name: /^Save$/ }));
+
+ await waitFor(() => expect(screen.queryByText(/Discard unsaved changes/)).toBeNull());
+ expect(currentSearch()).toContain('skill=code-fixer');
+ });
+
+ it('disarms the prompt when the already-open skill is clicked', async () => {
+ await openDirtyEditor();
+ fireEvent.click(screen.getByText('doc-writer'));
+
+ fireEvent.click(screen.getByText('code-fixer'));
+
+ expect(screen.queryByText(/Discard unsaved changes/)).toBeNull();
+ expect(screen.getByDisplayValue('# edited by hand')).toBeTruthy();
+ });
+
+ it('does not adopt an in-flight save as the next skill\'s baseline', async () => {
+ let resolveSave;
+ saveJobSkill.mockImplementationOnce(() => new Promise((r) => { resolveSave = r; }));
+ await openDirtyEditor();
+
+ fireEvent.click(screen.getByRole('button', { name: /^Save$/ }));
+ // Switch away while the PUT is still open, then let it land.
+ fireEvent.click(screen.getByText('doc-writer'));
+ fireEvent.click(screen.getByRole('button', { name: 'Discard' }));
+ await screen.findByText('Job doc-writer');
+ resolveSave({ success: true });
+
+ await waitFor(() => expect(toast.success).toHaveBeenCalled());
+ // The old skill's text must not become the new skill's clean baseline.
+ expect(screen.queryByText('Unsaved changes')).toBeNull();
+ });
+
it('does not prompt when a clean editor switches skills', async () => {
renderPage('/prompts?tab=job-skills&skill=code-fixer');
await screen.findByText('Job code-fixer');