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: clarify scheduled prompt upgrades while preserving user pins (#6765)

[._.]/ Adam Eivya55a5e04b03a
diff --git a/server/services/taskPromptDefaults.js b/server/services/taskPromptDefaults.js
index 59d28a242f..d47b7f6001 100644
--- a/server/services/taskPromptDefaults.js
+++ b/server/services/taskPromptDefaults.js
@@ -30,3 +30,4 @@
export { DEFAULT_TASK_PROMPTS } from './taskPromptDefaults/prompts.js';
export { PROMPT_VERSIONS, REFERENCE_WATCH_AUDITED_VERSION } from './taskPromptDefaults/versions.js';
export { promptMatchesShippedDefault } from './taskPromptDefaults/shippedPrompts.js';
+export { reconcileStoredPrompt, stampPromptWrite } from './taskPromptDefaults/storedPrompt.js';
diff --git a/server/services/taskPromptDefaults/storedPrompt.js b/server/services/taskPromptDefaults/storedPrompt.js
new file mode 100644
index 0000000000..48aef438fe
--- /dev/null
+++ b/server/services/taskPromptDefaults/storedPrompt.js
@@ -0,0 +1,59 @@
+import { DEFAULT_TASK_PROMPTS } from './prompts.js';
+import { PROMPT_VERSIONS } from './versions.js';
+import { promptMatchesShippedDefault } from './shippedPrompts.js';
+
+/** Reconcile shipped defaults and inferred customizations without overwriting explicit user pins. */
+export function reconcileStoredPrompt(config, taskType) {
+ const currentPrompt = DEFAULT_TASK_PROMPTS[taskType];
+ const currentVersion = PROMPT_VERSIONS[taskType] || 1;
+ const fields = Object.fromEntries(
+ ['prompt', 'promptVersion', 'promptCustomized', 'promptSource']
+ .filter(key => Object.hasOwn(config, key))
+ .map(key => [key, config[key]])
+ );
+ const finish = () => ({
+ ...fields,
+ changed: Object.keys(fields).some(key => fields[key] !== config[key])
+ });
+
+ // Missing body: install the current default and clear stale provenance.
+ if (!fields.prompt && currentPrompt) {
+ fields.prompt = currentPrompt;
+ fields.promptVersion = currentVersion;
+ if (fields.promptSource) fields.promptSource = null;
+ return finish();
+ }
+
+ const isShippedDefault = promptMatchesShippedDefault(fields.prompt, taskType);
+ const isCurrentDefault = fields.prompt === currentPrompt;
+
+ // Legacy unversioned body: recognize defaults before inferring a customization.
+ if (fields.prompt && fields.promptVersion === undefined && currentPrompt) {
+ fields.promptVersion = isCurrentDefault || !isShippedDefault ? currentVersion : 1;
+ if (!isShippedDefault) {
+ fields.promptCustomized = true;
+ fields.promptSource = 'legacy-inferred';
+ }
+ }
+
+ // Inferred pin of a shipped body: repair both the flag and a misleading version.
+ if (fields.promptSource !== 'user' && fields.promptCustomized && isShippedDefault) {
+ fields.promptCustomized = false;
+ if (!isCurrentDefault) fields.promptVersion = 1;
+ }
+
+ // Unpinned older version: replace the body with this release's default.
+ if (PROMPT_VERSIONS[taskType] && !fields.promptCustomized
+ && (fields.promptVersion || 1) < currentVersion) {
+ fields.prompt = currentPrompt;
+ fields.promptVersion = currentVersion;
+ }
+ return finish();
+}
+
+/** A Settings save pins only when the body is not the current default. */
+export function stampPromptWrite(prompt, taskType) {
+ const body = typeof prompt === 'string' && !prompt.trim() ? null : prompt;
+ const promptCustomized = body != null && body !== DEFAULT_TASK_PROMPTS[taskType];
+ return { prompt: body, promptCustomized, promptSource: promptCustomized ? 'user' : null };
+}
diff --git a/server/services/taskPromptDefaults/storedPrompt.test.js b/server/services/taskPromptDefaults/storedPrompt.test.js
new file mode 100644
index 0000000000..ab5ced45d3
--- /dev/null
+++ b/server/services/taskPromptDefaults/storedPrompt.test.js
@@ -0,0 +1,62 @@
+import { describe, expect, it } from 'vitest';
+import { reconcileStoredPrompt, stampPromptWrite } from './storedPrompt.js';
+import { DEFAULT_TASK_PROMPTS } from './prompts.js';
+import { PROMPT_VERSIONS } from './versions.js';
+import { RETIRED_CONSOLE_ERRORS_PROMPT } from './retiredPromptFixtures.js';
+
+const taskType = 'console-errors';
+const prompt = DEFAULT_TASK_PROMPTS[taskType];
+const promptVersion = PROMPT_VERSIONS[taskType];
+const retired = RETIRED_CONSOLE_ERRORS_PROMPT;
+
+describe('stored prompt compatibility cases', () => {
+ it.each([
+ ['missing prompt installs the default and clears stale provenance',
+ { prompt: null, promptSource: 'user' },
+ { prompt, promptVersion, promptSource: null, changed: true }],
+ ['unversioned current default is stamped without rewriting',
+ { prompt }, { prompt, promptVersion, changed: true }],
+ ['unversioned retired default resets its version and upgrades',
+ { prompt: retired }, { prompt, promptVersion, changed: true }],
+ ['unrecognized unversioned body becomes an inferred pin',
+ { prompt: 'my instructions' },
+ { prompt: 'my instructions', promptVersion, promptCustomized: true, promptSource: 'legacy-inferred', changed: true }],
+ ['legacy-inferred retired pin self-heals even with the current version',
+ { prompt: retired, promptVersion, promptCustomized: true, promptSource: 'legacy-inferred' },
+ { prompt, promptVersion, promptCustomized: false, promptSource: 'legacy-inferred', changed: true }],
+ ['absent provenance on a current default self-heals',
+ { prompt, promptVersion, promptCustomized: true },
+ { prompt, promptVersion, promptCustomized: false, changed: true }],
+ ['explicit user pin of a retired default survives a version bump (#5432)',
+ { prompt: retired, promptVersion: 1, promptCustomized: true, promptSource: 'user' },
+ { prompt: retired, promptVersion: 1, promptCustomized: true, promptSource: 'user', changed: false }],
+ ['older uncustomized version upgrades',
+ { prompt: retired, promptVersion: 1, promptCustomized: false },
+ { prompt, promptVersion, promptCustomized: false, changed: true }],
+ ['current default with a current version needs no save',
+ { prompt, promptVersion }, { prompt, promptVersion, changed: false }],
+ ['unknown task without a shipped default is untouched',
+ { prompt: 'custom task', promptVersion: 2 },
+ { prompt: 'custom task', promptVersion: 2, changed: false }, 'unknown-task']
+ ])('%s', (_name, config, expected, type = taskType) => {
+ const original = structuredClone(config);
+ expect(reconcileStoredPrompt(config, type)).toEqual(expected);
+ expect(config).toEqual(original);
+ const { changed: _changed, ...reconciled } = expected;
+ expect(reconcileStoredPrompt(reconciled, type)).toEqual({ ...reconciled, changed: false });
+ });
+});
+
+describe('Settings prompt writes', () => {
+ it.each([
+ ['empty body resumes defaults', ' \n', null, false],
+ ['cleared body resumes defaults', null, null, false],
+ ['re-save of current default does not pin', prompt, prompt, false],
+ ['retired shipped body is an explicit pin', retired, retired, true],
+ ['custom body is an explicit pin', 'my instructions', 'my instructions', true]
+ ])('%s', (_name, input, body, pinned) => {
+ expect(stampPromptWrite(input, taskType)).toEqual({
+ prompt: body, promptCustomized: pinned, promptSource: pinned ? 'user' : null
+ });
+ });
+});
diff --git a/server/services/taskSchedule.js b/server/services/taskSchedule.js
index a7e3dbdf65..b8e8838ea4 100644
--- a/server/services/taskSchedule.js
+++ b/server/services/taskSchedule.js
@@ -27,7 +27,7 @@ import { getLocalParts } from '../lib/timezone.js';
import { getUserTimezone } from './userTimezone.js';
import { parseCronToNextRun } from './eventScheduler.js';
import { isAuditTaskType, defaultFileIssuesFor, auditDoWorkRequiresWorktree, getAuditScheduleMetadata, AUDIT_RUN_GUIDANCE, AUDIT_SUGGESTED_AFTER } from '../lib/auditCatalog.js';
-import { DEFAULT_TASK_PROMPTS } from './taskPromptDefaults.js';
+import { DEFAULT_TASK_PROMPTS, stampPromptWrite } from './taskPromptDefaults.js';
import {
DEFAULT_PERPETUAL_RECHECK_MS,
INTERVAL_TYPES,
@@ -191,10 +191,6 @@ export async function updateTaskInterval(taskType, settings) {
schedule.tasks[taskType] = { type: INTERVAL_TYPES.ON_DEMAND, perpetual: false, enabled: false, providerId: null, model: null, createdAt: new Date().toISOString() };
}
- // Normalize empty/whitespace prompts to null (treated as "use default")
- if ('prompt' in settings && typeof settings.prompt === 'string' && !settings.prompt.trim()) {
- settings.prompt = null;
- }
// The description is display-only schedule metadata. Keep it separate from
// the prompt so custom card copy never becomes agent instructions.
if ('description' in settings) {
@@ -202,24 +198,8 @@ export async function updateTaskInterval(taskType, settings) {
? settings.description.trim().slice(0, 240) || null
: null;
}
- // If user is setting a custom prompt, mark it so auto-upgrade won't overwrite it.
- // If user clears the prompt (null), remove the customized flag to resume defaults.
- //
- // `promptSource: 'user'` records that this write was an EXPLICIT user action,
- // which is what the store's self-heal reads to leave the pin alone (#5432).
- // Without it, pasting an older SHIPPED body into Settings → Scheduled Tasks was
- // un-pinnable: the self-heal saw a body matching a retired default, cleared the
- // flag on the next load, and the next PROMPT_VERSIONS bump overwrote the text.
- //
- // A body identical to the CURRENT default is not a pin. The editor prefills its
- // textarea from the stored prompt, so re-saving an untouched default would
- // otherwise stamp a permanent pin and freeze that type off every future prompt
- // upgrade — which is exactly the mis-flag the self-heal existed to undo, now
- // beyond its reach. Retired shipped bodies still pin: that is the #5432 case.
if ('prompt' in settings) {
- settings.promptCustomized = settings.prompt != null
- && settings.prompt !== DEFAULT_TASK_PROMPTS[taskType];
- settings.promptSource = settings.promptCustomized ? 'user' : null;
+ Object.assign(settings, stampPromptWrite(settings.prompt, taskType));
}
schedule.tasks[taskType] = {
diff --git a/server/services/taskScheduleStore.js b/server/services/taskScheduleStore.js
index 7a1ab63688..befdc5ae4a 100644
--- a/server/services/taskScheduleStore.js
+++ b/server/services/taskScheduleStore.js
@@ -15,8 +15,7 @@ import {
} from './taskScheduleRegistry.js';
import {
DEFAULT_TASK_PROMPTS,
- PROMPT_VERSIONS,
- promptMatchesShippedDefault
+ reconcileStoredPrompt
} from './taskPromptDefaults.js';
const DATA_DIR = PATHS.cos;
@@ -252,85 +251,13 @@ async function readSchedule() {
config.createdAt = new Date().toISOString();
needsSave = true;
}
- if (!config.prompt && DEFAULT_TASK_PROMPTS[taskType]) {
- // No prompt set — initialize with current default and version
- config.prompt = DEFAULT_TASK_PROMPTS[taskType];
- config.promptVersion = PROMPT_VERSIONS[taskType] || 1;
- // A prompt-less config pins nothing, so drop any stale provenance rather
- // than let it freeze the freshly-installed default off the upgrade path.
- if (config.promptSource) config.promptSource = null;
- needsSave = true;
- } else {
- // Legacy migration: infer customization when promptVersion is missing
- if (
- config.prompt &&
- config.promptVersion === undefined &&
- DEFAULT_TASK_PROMPTS[taskType]
- ) {
- if (config.prompt === DEFAULT_TASK_PROMPTS[taskType]) {
- // Matches current default — assign current version (no upgrade needed)
- config.promptVersion = PROMPT_VERSIONS[taskType] || 1;
- needsSave = true;
- } else if (promptMatchesShippedDefault(config.prompt, taskType)) {
- // Matches a known previous default — assign version 1 so auto-upgrade triggers
- config.promptVersion = 1;
- needsSave = true;
- } else {
- // Prompt differs from all known defaults — treat as user-customized.
- // Stamp the provenance as INFERRED, not 'user': this branch is a guess
- // made from a body we don't recognize, so the self-heal below must stay
- // free to undo it once the body turns out to be a retired default.
- config.promptCustomized = true;
- config.promptSource = 'legacy-inferred';
- config.promptVersion = PROMPT_VERSIONS[taskType] || 1;
- needsSave = true;
- }
- }
-
- // Self-heal a mis-flagged customization: a prompt marked promptCustomized
- // that nonetheless matches a shipped default was never user-edited — it
- // was flagged by an earlier legacy migration that ran before this task
- // carried a retired-default history entry (the basic self-improvement
- // prompts that hardcoded the app name as "PortOS", and both
- // pre-unification generations — `[Self-Improvement] …` and
- // `[App Improvement: …]` — that the schedule unification replaced without
- // preserving). Clear the flag so the auto-upgrade below can replace the
- // stale default.
- //
- // Clearing the flag is NOT enough on its own. That legacy migration
- // stamped `promptVersion = PROMPT_VERSIONS[taskType]` alongside the flag,
- // so an install flagged after a type was versioned carries the CURRENT
- // version while holding a RETIRED body — the upgrade below then sees
- // `storedVersion < current` as false and leaves the stale prompt in place
- // forever, now un-flagged so nothing else notices. Reset the version to 1
- // whenever the body is a prior default rather than the current one, which
- // is the same stamp the version-inference branch above applies.
- //
- // Gated on provenance (#5432): a user who deliberately pastes an older
- // SHIPPED body into Settings → Scheduled Tasks also byte-matches a shipped
- // default, and clearing THAT flag would let the next PROMPT_VERSIONS bump
- // overwrite their chosen text. `promptSource === 'user'` marks an explicit
- // write through updateTaskInterval and is left alone; 'legacy-inferred' and
- // absent (every install upgrading into this field) self-heal exactly as
- // they do today.
- if (config.promptSource !== 'user'
- && config.promptCustomized
- && promptMatchesShippedDefault(config.prompt, taskType)) {
- config.promptCustomized = false;
- if (config.prompt !== DEFAULT_TASK_PROMPTS[taskType]) config.promptVersion = 1;
- needsSave = true;
- }
-
- if (PROMPT_VERSIONS[taskType] && !config.promptCustomized) {
- // Auto-upgrade non-customized prompts when code version is newer
- const storedVersion = config.promptVersion || 1;
- if (storedVersion < PROMPT_VERSIONS[taskType]) {
- emitLog('info', `Upgrading ${taskType} prompt v${storedVersion} → v${PROMPT_VERSIONS[taskType]}`, { taskType }, '📅 TaskSchedule');
- config.prompt = DEFAULT_TASK_PROMPTS[taskType];
- config.promptVersion = PROMPT_VERSIONS[taskType];
- needsSave = true;
- }
+ const { changed, ...promptFields } = reconcileStoredPrompt(config, taskType);
+ if (changed) {
+ if (config.prompt && config.prompt !== promptFields.prompt) {
+ emitLog('info', `Upgrading ${taskType} prompt to v${promptFields.promptVersion}`, { taskType }, '📅 TaskSchedule');
}
+ Object.assign(config, promptFields);
+ needsSave = true;
}
}