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: migration 270 must match the parser on key case and heading spill (#4153)

Adam Eivye8be64f2b42e
diff --git a/scripts/migrations/270-cos-task-prompt-split.js b/scripts/migrations/270-cos-task-prompt-split.js
index 4296716288..95a9d8d612 100644
--- a/scripts/migrations/270-cos-task-prompt-split.js
+++ b/scripts/migrations/270-cos-task-prompt-split.js
@@ -127,23 +127,36 @@ export function splitPromptMetadata(markdown, { stamp }) {
for (const line of lines) {
const header = line.match(TASK_LINE);
- // A task's block runs to the NEXT task header or section heading — not to
- // the first non-metadata line. A description written with embedded newlines
- // is interpolated into the file verbatim by `generateTasksMarkdown`, so a
- // freshly-filed task can carry blank/prose lines between its header and its
- // metadata (they are dropped on the next parse round-trip, but a migration
- // may well run before that happens).
- if (header || line.startsWith('#')) {
+ // A task's block runs to the NEXT task header — NOT to the first
+ // non-metadata line, and NOT to a `#` heading. Two reasons, both load-bearing
+ // for exactly the payloads this migration targets:
+ // - A description written with embedded newlines is interpolated into the
+ // file verbatim by `generateTasksMarkdown`, so a freshly-filed task can
+ // carry blank/prose lines between its header and its metadata (they are
+ // dropped on the next parse round-trip, but a migration may well run
+ // before that happens).
+ // - Those spilled lines are usually MARKDOWN HEADINGS (`## Phase 1` — the
+ // generator's Phase 1–7 body is the canonical case). Ending the block at
+ // a `#` line would walk right past the `context:` line below it.
+ // `parseTasksMarkdown` does the same: a `##` section heading advances the
+ // section but never clears `currentTask`, so metadata after one still
+ // attaches to the preceding task. Mirror it, or this rewrite and the parser
+ // would disagree about which task owns a line.
+ if (header) {
finish();
out.push(line);
- if (header) taskId = header[2];
+ taskId = header[2];
continue;
}
const meta = taskId ? line.match(METADATA_LINE) : null;
if (meta) {
indent = meta[1];
lastMetaAt = out.length;
- const key = meta[2];
+ // Normalize the key exactly as `parseMetadataLine` does — legacy
+ // Title-Case keys (`Context`, `Prompt`) are real on older installs and
+ // read back as the camelCase key, so a case-sensitive compare here would
+ // skip them, record the migration applied, and leave them unsplit forever.
+ const key = meta[2].charAt(0).toLowerCase() + meta[2].slice(1);
if (key === TASK_PROMPT_KEY) hasPrompt = true;
else if (key === TASK_CONTEXT_KEY) {
const decoded = decodeMetadataValue(meta[3].trim());
diff --git a/scripts/migrations/270-cos-task-prompt-split.test.js b/scripts/migrations/270-cos-task-prompt-split.test.js
index 73079f571c..dc9927d7a2 100644
--- a/scripts/migrations/270-cos-task-prompt-split.test.js
+++ b/scripts/migrations/270-cos-task-prompt-split.test.js
@@ -104,6 +104,60 @@ describe('splitPromptMetadata', () => {
expect(markdown).toContain('Some prose the producer left behind.');
});
+ // `parseMetadataLine` normalizes legacy Title-Case keys, so an old install's
+ // `- Context:` line reads back as `metadata.context`. A case-sensitive compare
+ // here would skip it, record the migration applied, and leave it unsplit
+ // forever.
+ it('migrates a legacy Title-Case Context key', () => {
+ const titleCase = [
+ '# Tasks',
+ '',
+ '## Pending',
+ '- [ ] #sys-1 | MEDIUM | AUTO | Improve Example App',
+ ' - Context: __json__:"line one\\nline two"',
+ '',
+ ].join('\n');
+ const { markdown, split } = splitPromptMetadata(titleCase, { stamp: STAMP });
+ expect(split).toEqual(['sys-1']);
+ expect(parseTasksMarkdown(markdown)[0].metadata.prompt).toBe('line one\nline two');
+ });
+
+ it('skips a task whose existing prompt is spelled Title-Case', () => {
+ const titleCase = [
+ '# Tasks',
+ '',
+ '## Pending',
+ '- [ ] #sys-1 | MEDIUM | AUTO | Improve Example App',
+ ' - Prompt: already split',
+ ' - context: __json__:"line one\\nline two"',
+ '',
+ ].join('\n');
+ expect(splitPromptMetadata(titleCase, { stamp: STAMP }))
+ .toEqual({ markdown: titleCase, split: [] });
+ });
+
+ // The payloads this migration targets are generated agent bodies whose
+ // continuation lines are markdown headings (`## Phase 1`). Ending the block
+ // scan at a `#` line would walk right past the `context:` line below it —
+ // and `parseTasksMarkdown` does not end a task there either.
+ it('reaches the metadata past a spilled description made of markdown headings', () => {
+ const spilled = [
+ '# Tasks',
+ '',
+ '## Pending',
+ '- [ ] #sys-1 | MEDIUM | AUTO | Improve Example App',
+ '',
+ '## Phase 1',
+ 'Read PLAN.md.',
+ ' - context: __json__:"line one\\nline two"',
+ '',
+ ].join('\n');
+ const { markdown, split } = splitPromptMetadata(spilled, { stamp: STAMP });
+ expect(split).toEqual(['sys-1']);
+ expect(markdown).toContain('- prompt: __json__:"line one\\nline two"');
+ expect(markdown).toContain('## Phase 1');
+ });
+
it('handles a legacy pre-sentinel escaped value', () => {
const legacy = [
'# Tasks',