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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: stop generated agent prompts double-printing their first line
[._.]/ Adam Eivy79d64952bef4
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex 85fadf1c79..9e3364adbd 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -10,6 +10,8 @@## Fixes+- **Generated agent prompts (swarm, scheduled claim-work, other queued system tasks) no longer double-print their first line.** A task queued through the CoS improvement/claim-work path is persisted to `COS-TASKS.md` by moving its full multi-line prompt into `metadata.context` and keeping only the first line as `description` (so the markdown stays one-line-per-task). Coming back out, `context` therefore led with a verbatim copy of `description`, and every prompt-render path emitted both — so a swarm run opened with the header twice (`# ⚡ SWARM MODE …`, then `### Context`, then `# ⚡ SWARM MODE …` again, the whole body repeated). A new `reconcileSplitContext` step in `agentPromptBuilder.js` detects that exact split signature (context's first non-empty line equals `description`), folds the body back into `description`, and drops the redundant `metadata.context` — so the prompt renders once as one clean body with no spurious `### Context` wrapper. A genuinely-separate user-supplied context (whose first line differs from the description) is left untouched, and the stored task keeps its one-line description for the task-list UI. Pure code fix — reaches every install on update, no data or prompt-template migration.+- **The CoS agent briefing no longer prints a redundant "Target Application" heading for the PortOS default app on the api-prompt path (#2430).** The light/fallback prompt path already suppressed the target-app line for `portos-default` (the agent runs in the PortOS directory, so it adds nothing), but the editable `cos-agent-briefing` stage template still rendered the heading for any task carrying an app id. The template now gates the stock heading on a precomputed `targetAppLabel` (empty for the default app, the app id for managed apps) that `agentPromptBuilder.js` threads into the render context — mirroring `buildTaskBlock`'s predicate — while leaving `task.metadata.app` in the context untouched for any custom template references. Ships as stage-prompt migration 187 (hash-matched, cross-synced with 010's accepted-hash chain) so unmodified installs auto-upgrade and customized copies are left alone.- **Layered Intelligence's outcome feedback loop now reconciles the `plan` (PLAN.md) tracker too (#2435).** After #2428 closed the loop for forge (gh/glab) and jira trackers, the `plan` tracker stayed excluded because `readIssues` mapped every `[lil-*]` PLAN.md item to `state: 'open'` regardless of its `- [ ]` / `- [x]` checkbox, so a completed proposal could never resolve. `extractPlanSlugs` now preserves each item's checkbox state — a checked `- [x]` reads `closed` (which `deriveOutcome` maps to *merged*) and an unchecked `- [ ]` (or a bare tag with no checkbox — absent ≠ done) reads `open` — so a checked item both reconciles as an outcome and falls out of the dedup window to become re-proposable, while open items stay suppressed as before. The `outcomesTrackerSupported()` gate now includes `plan`, so PLAN-tracked apps learn from their own merge rate like forge/jira apps.diff --git a/server/services/agentPromptBuilder.js b/server/services/agentPromptBuilder.jsindex 0d00a8e311..7b00624b0c 100644--- a/server/services/agentPromptBuilder.js+++ b/server/services/agentPromptBuilder.js@@ -232,6 +232,37 @@ export function buildTaskBlock(task, { screenshotsAsList = false } = {}) {return { description, targetApp, screenshots, attachments };}+/**+ * Undo the COS-TASKS.md round-trip split before rendering. The queue path in+ * `cosTaskGenerator.js` persists a generated multi-line prompt by moving the+ * full body into `metadata.context` and keeping only its first line as+ * `description`, so the markdown stays one-line-per-task. Coming back out,+ * `context` therefore *leads with a verbatim copy* of `description` — and every+ * render path emits both (the task block, then the full body again under a+ * `### Context` header). For a swarm task that surfaces as the reported+ * double `# ⚡ SWARM MODE …` header; the same duplication hits any other+ * generated/scheduled/system task that round-trips through the queue.+ *+ * When the split signature is present (`context` is a string whose first+ * non-empty line equals `description`), fold `context` back into `description`+ * and drop the redundant `metadata.context` so the prompt renders once, as one+ * clean body with no spurious header. A genuinely-separate user-supplied+ * `context` (first line differs from `description`) is left untouched.+ *+ * Pure and idempotent: returns the same task when nothing matched, otherwise a+ * shallow clone (never mutates the caller's task, so the stored task keeps its+ * one-line `description` for the task-list UI).+ */+export function reconcileSplitContext(task) {+ const context = task?.metadata?.context;+ if (typeof context !== 'string' || typeof task.description !== 'string') return task;+ // Mirror firstLine() in cosTaskStore.js: first non-empty, trimmed line.+ const firstNonEmpty = context.split('\n').map(l => l.trim()).find(Boolean) || '';+ if (firstNonEmpty !== task.description.trim()) return task;+ const { context: _dropped, ...restMeta } = task.metadata;+ return { ...task, description: context, metadata: restMeta };+}+/*** Build the **review-loop follow-up** section — the instructions for the* agent spawned by `spawnReviewLoopFollowUp` to drive Copilot's review-and-fix@@ -549,6 +580,12 @@ export function buildReadOnlyCompletionSection({ isTui = false, sentinelPath = n* Ignored on the full/api path, which always returns a string.*/export async function buildAgentPrompt(task, config, workspaceDir, worktreeInfo = null, isTruthyMetaFn = (v) => v === true || v === 'true', options = {}) {+ // Undo the queue-path description/context split so a round-tripped generated+ // prompt (swarm, scheduled claim-work, other system tasks) renders once+ // instead of double-printing its first line under a `### Context` header.+ // Feeds both the briefing template (via the reconciled `task` object) and the+ // full-path fallback below.+ task = reconcileSplitContext(task);const providerType = options.providerType || PROVIDER_TYPES.API;const providerId = options.providerId || null;const providerCommand = options.providerCommand || null;@@ -895,6 +932,9 @@ export function buildLightContextPromptParts(task, workspaceDir, worktreeInfo, iconst BEGIN_WORKING_LINE = 'Begin working on the task now.';function buildLightContextSections(task, workspaceDir, worktreeInfo, isTruthyMetaFn, { isTui = true, providerId = null, providerCommand = null, leanMode = false } = {}) {+ // Idempotent with the reconcile in buildAgentPrompt; also protects the+ // directly-exported buildLightContextPrompt/Parts entry points.+ task = reconcileSplitContext(task);const willOpenPR = isTruthyMetaFn(task.metadata?.openPR);const willReviewLoop = isTruthyMetaFn(task.metadata?.reviewLoop);const simplifyEnabled = isTruthyMetaFn(task.metadata?.simplify);diff --git a/server/services/agentPromptBuilder.test.js b/server/services/agentPromptBuilder.test.jsindex a506f3f5a1..4ee1b903f2 100644--- a/server/services/agentPromptBuilder.test.js+++ b/server/services/agentPromptBuilder.test.js@@ -55,7 +55,7 @@ tryReadFile: vi.fn().mockResolvedValue(null),createTicket: vi.fn().mockResolvedValue(null),}));-import { buildLightContextPrompt, buildAgentPrompt, buildCompletionGuidelineBullet } from './agentPromptBuilder.js';+import { buildLightContextPrompt, buildAgentPrompt, buildCompletionGuidelineBullet, reconcileSplitContext } from './agentPromptBuilder.js';import { isTruthyMeta } from './agentState.js';import { buildPrompt } from './promptService.js'; // mocked above — inspect call args@@ -69,6 +69,37 @@ function makeTask(overrides = {}) {};}+describe('reconcileSplitContext', () => {+ it('folds context back into description when it is the queue-path split', () => {+ const body = 'Line one is the title\n\nLine two is the body.';+ const task = { description: 'Line one is the title', metadata: { context: body, app: 'comics' } };+ const out = reconcileSplitContext(task);+ expect(out.description).toBe(body);+ expect(out.metadata.context).toBeUndefined();+ expect(out.metadata.app).toBe('comics'); // other metadata preserved+ });++ it('leaves a genuinely-separate user context untouched', () => {+ const task = { description: 'Add a button', metadata: { context: 'Unrelated context detail' } };+ const out = reconcileSplitContext(task);+ expect(out).toBe(task);+ expect(out.metadata.context).toBe('Unrelated context detail');+ });++ it('does not mutate the caller task (stored one-line description survives)', () => {+ const body = 'Title\n\nBody.';+ const task = { description: 'Title', metadata: { context: body } };+ reconcileSplitContext(task);+ expect(task.description).toBe('Title');+ expect(task.metadata.context).toBe(body);+ });++ it('is idempotent and a no-op when there is no context', () => {+ const task = { description: 'Title', metadata: {} };+ expect(reconcileSplitContext(task)).toBe(task);+ });+});+describe('buildLightContextPrompt', () => {describe('what it omits', () => {it('does NOT include the obsolete "# Chief of Staff Agent Briefing" header', () => {@@ -135,6 +166,21 @@ describe('buildLightContextPrompt', () => {expect(multi).toMatch(/### Context\n\nline one\nline two/);});+ it('does NOT double-render a queue-path split prompt (description == firstLine(context))', () => {+ // The queue path stores `description = firstLine(body)` + `context = body`+ // so COS-TASKS.md stays one-line-per-task. Rendering both would print the+ // first line twice (the reported double `# ⚡ SWARM MODE …` header).+ const body = '# ⚡ SWARM MODE — claim and ship up to 3 independent issues in parallel\n\n**This run operates in slashdo mode.** Do the work.';+ const prompt = buildLightContextPrompt(+ makeTask({ description: '# ⚡ SWARM MODE — claim and ship up to 3 independent issues in parallel', metadata: { context: body } }),+ '/r', null, isTruthyMeta);+ // Header appears exactly once, and the redundant `### Context` wrapper is gone.+ expect(prompt.match(/# ⚡ SWARM MODE/g)).toHaveLength(1);+ expect(prompt).not.toMatch(/### Context/);+ // The full body still renders (nothing dropped).+ expect(prompt).toMatch(/\*\*This run operates in slashdo mode\.\*\* Do the work\./);+ });+it('lists screenshot file paths so the agent can read them via its own tools', () => {const prompt = buildLightContextPrompt(makeTask({ metadata: { screenshots: ['/tmp/a.png', '/tmp/b.png'] } }),