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(cos): preserve multi-line context in agent resume tasks

Adam Eivy25c363aca197
diff --git a/.changelog/v0.10.x.md b/.changelog/v0.10.x.md
index 15eccf94b2..ccfbfa4a85 100644
--- a/.changelog/v0.10.x.md
+++ b/.changelog/v0.10.x.md
@@ -203,6 +203,12 @@ This release introduces a structured changelog documentation system for creating
- **Fixed ENOENT error in task prompt enhancement**: The `taskEnhancer.js` was creating its own runId (`enhance-xxx`) and passing it to `createRun`, but the AI Toolkit's `createRun` ignores passed IDs and generates UUIDs. When `executeApiRun` was then called with the original `enhance-xxx` ID, the directory didn't exist.
- **Solution**: Now uses the `runId` returned from `createRun({ ... })` instead of generating a separate ID
+### Fixed Multi-Line Context Loss in Agent Resume
+- **Root Cause**: When resuming agent tasks, the "Additional Instructions" and "Previous Agent Context" provided in the resume form were truncated to just the first line (e.g., only "## Additional Instructions" was saved)
+- **The Fix**: Task metadata values containing newlines are now properly escaped (`\n` → `\\n`) when saving to the TASKS.md file and unescaped when parsing back
+- **Impact**: Resume context now correctly passes full multi-line instructions to agents, including previous agent output, error context, and user refinements
+- **Test Coverage**: Added new round-trip test to verify multi-line metadata preservation through save/parse cycle
+
### Critical: Fixed "Unexpected end of JSON input" Errors
- **Safe JSON Parsing**: Added comprehensive JSON parsing utilities to `server/lib/fileUtils.js`:
- `isValidJSON(str)` - Pre-validates JSON structure before parsing (supports both objects and arrays)
diff --git a/server/lib/taskParser.js b/server/lib/taskParser.js
index 90bcf6d7c5..bd28c84cbe 100644
--- a/server/lib/taskParser.js
+++ b/server/lib/taskParser.js
@@ -87,6 +87,22 @@ function parseTaskLine(line) {
};
}
+/**
+ * Unescape newlines in metadata values
+ * Converts \\n back to actual newlines
+ */
+function unescapeNewlines(value) {
+ return value.replace(/\\n/g, '\n');
+}
+
+/**
+ * Escape newlines in metadata values
+ * Converts newlines to \\n for single-line storage
+ */
+function escapeNewlines(value) {
+ return value.replace(/\n/g, '\\n');
+}
+
/**
* Parse metadata line (indented under task)
* Format: - Key: Value
@@ -97,7 +113,7 @@ function parseMetadataLine(line) {
return {
key: match[1].toLowerCase(),
- value: match[2].trim()
+ value: unescapeNewlines(match[2].trim())
};
}
@@ -205,10 +221,11 @@ export function generateTasksMarkdown(tasks, includeApprovalFlags = false) {
: '';
lines.push(`- ${checkbox} #${task.id} | ${task.priority}${approvalFlag} | ${task.description}`);
- // Add metadata
+ // Add metadata (escape newlines in values for single-line storage)
for (const [key, value] of Object.entries(task.metadata)) {
const capitalizedKey = key.charAt(0).toUpperCase() + key.slice(1);
- lines.push(` - ${capitalizedKey}: ${value}`);
+ const escapedValue = escapeNewlines(String(value));
+ lines.push(` - ${capitalizedKey}: ${escapedValue}`);
}
}
diff --git a/server/lib/taskParser.test.js b/server/lib/taskParser.test.js
index 6e0118955d..d1093d373b 100644
--- a/server/lib/taskParser.test.js
+++ b/server/lib/taskParser.test.js
@@ -265,6 +265,23 @@ describe('Task Parser', () => {
expect(markdown).toContain('- App: my-app');
});
+ it('should escape newlines in metadata values for round-trip preservation', () => {
+ const multiLineContext = '## Additional Instructions\nFix the bug\n\n## Previous Context\nAgent ID: agent-123';
+ const tasks = [
+ { id: 'task-001', status: 'pending', priority: 'MEDIUM', priorityValue: 2, description: 'Resume task', metadata: { context: multiLineContext } }
+ ];
+
+ const markdown = generateTasksMarkdown(tasks);
+
+ // Should contain escaped newlines
+ expect(markdown).toContain('\\n');
+ expect(markdown).not.toContain('\n## Additional');
+
+ // Round-trip test: parse it back and verify context is preserved
+ const parsed = parseTasksMarkdown(markdown);
+ expect(parsed[0].metadata.context).toBe(multiLineContext);
+ });
+
it('should sort tasks by priority within sections', () => {
const tasks = [
{ id: 'task-001', status: 'pending', priority: 'LOW', priorityValue: 1, description: 'Low', metadata: {} },