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(cos): address Copilot review feedback for openPR feature
Adam Eivy4939bfaaa73c
diff --git a/client/src/components/cos/TaskAddForm.jsx b/client/src/components/cos/TaskAddForm.jsxindex 5cda3ba556..939d10a723 100644--- a/client/src/components/cos/TaskAddForm.jsx+++ b/client/src/components/cos/TaskAddForm.jsx@@ -49,9 +49,11 @@ export default function TaskAddForm({ providers, apps, onTaskAdded, compact = fa// Auto-toggle JIRA, worktree, and PR checkboxes when app selection changesuseEffect(() => {const app = apps?.find(a => a.id === newTask.app);+ const defaultOpenPR = !!app?.defaultOpenPR;+ const defaultUseWorktree = !!app?.defaultUseWorktree || defaultOpenPR;setCreateJiraTicket(!!app?.jira?.enabled);- setUseWorktree(!!app?.defaultUseWorktree);- setOpenPR(!!app?.defaultOpenPR);+ setUseWorktree(defaultUseWorktree);+ setOpenPR(defaultOpenPR);}, [newTask.app, apps]);// Get models for selected providerdiff --git a/server/lib/validation.test.js b/server/lib/validation.test.jsindex 9643d8da9e..13cb1be34b 100644--- a/server/lib/validation.test.js+++ b/server/lib/validation.test.js@@ -453,6 +453,14 @@ describe('validation.js', () => {expect(sanitizeTaskMetadata({ useWorktree: true, simplify: false })).toEqual({ useWorktree: true, simplify: false });});+ it('should accept openPR as an allowed metadata key', () => {+ expect(sanitizeTaskMetadata({ openPR: true })).toEqual({ openPR: true });+ expect(sanitizeTaskMetadata({ openPR: false })).toEqual({ openPR: false });+ expect(sanitizeTaskMetadata({ useWorktree: true, openPR: true })).toEqual({ useWorktree: true, openPR: true });+ expect(sanitizeTaskMetadata({ useWorktree: true, openPR: true, simplify: true, reviewLoop: false }))+ .toEqual({ useWorktree: true, openPR: true, simplify: true, reviewLoop: false });+ });+it('should drop non-boolean values for allowed keys', () => {expect(sanitizeTaskMetadata({ useWorktree: 'yes' })).toBeNull();expect(sanitizeTaskMetadata({ simplify: 1 })).toBeNull();diff --git a/server/services/cos.js b/server/services/cos.jsindex 91db3369a3..3859ebf5e7 100644--- a/server/services/cos.js+++ b/server/services/cos.js@@ -1866,18 +1866,25 @@ Use model: claude-opus-4-5-20251101 for thorough security analysis`* @param {Object} state - Current CoS state* @returns {Object} Generated task*/-// When an app explicitly sets defaultUseWorktree/defaultOpenPR, override per-task-type metadata+// Apply app-level worktree/PR defaults only when not already set by task-type metadatafunction applyAppWorktreeDefault(metadata, app) {- if (app.defaultUseWorktree === true) {- metadata.useWorktree = true;- } else if (app.defaultUseWorktree === false) {- metadata.useWorktree = false;- metadata.openPR = false;+ if (metadata.useWorktree === undefined) {+ if (app.defaultUseWorktree === true) {+ metadata.useWorktree = true;+ } else if (app.defaultUseWorktree === false) {+ metadata.useWorktree = false;+ }+ }+ if (metadata.openPR === undefined && metadata.useWorktree !== false) {+ if (app.defaultOpenPR === true) {+ metadata.openPR = true;+ metadata.useWorktree = true; // openPR implies useWorktree+ } else if (app.defaultOpenPR === false) {+ metadata.openPR = false;+ }}- if (app.defaultOpenPR === true && metadata.useWorktree !== false) {- metadata.openPR = true;- metadata.useWorktree = true; // openPR implies useWorktree- } else if (app.defaultOpenPR === false) {+ // Ensure openPR=false when useWorktree=false (invariant: openPR implies useWorktree)+ if (metadata.useWorktree === false) {metadata.openPR = false;}}diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.jsindex 6e9bcd1d23..f723ed1f9b 100644--- a/server/services/subAgentSpawner.js+++ b/server/services/subAgentSpawner.js@@ -1981,7 +1981,7 @@ async function cleanupAgentWorktree(agentId, success, { openPR = false, descriptgit.getRepoBranches(sourceWorkspace).catch(() => ({ baseBranch: null, devBranch: null }))]);- // Only create PR if push succeeded+ // Only create PR if push succeeded; fall back to auto-merge if push failsif (pushResult) {const targetBranch = branchInfo.devBranch || branchInfo.baseBranch || 'main';const taskDesc = description || 'CoS automated task';@@ -1999,10 +1999,17 @@ async function cleanupAgentWorktree(agentId, success, { openPR = false, descriptif (prResult?.success) {emitLog('success', `🌳 Created PR: ${prResult.url}`, { agentId, branchName: worktreeBranch });}++ // Remove worktree without merging (PR handles merge)+ await removeWorktree(agentId, sourceWorkspace, worktreeBranch, { merge: false }).catch(err => {+ emitLog('warn', `🌳 Worktree cleanup failed for ${agentId}: ${err.message}`, { agentId });+ });+ return;}- // Remove worktree without merging (PR handles merge)- await removeWorktree(agentId, sourceWorkspace, worktreeBranch, { merge: false }).catch(err => {+ // Push failed — fall back to auto-merge so commits aren't lost+ emitLog('warn', `🌳 Push failed for ${worktreeBranch}, falling back to auto-merge`, { agentId });+ await removeWorktree(agentId, sourceWorkspace, worktreeBranch, { merge: true }).catch(err => {emitLog('warn', `🌳 Worktree cleanup failed for ${agentId}: ${err.message}`, { agentId });});return;@@ -2789,7 +2796,7 @@ ${skillSection ? `## Task-Type Skill Guidelines\n\n${skillSection}\n` : ''}${pla- Do not make unrelated changes- If blocked, explain clearly why- Never update the PortOS changelog (\`.changelog/\`) for work on managed apps — the PortOS changelog tracks PortOS core changes only-${task.metadata?.app && worktreeInfo && willOpenPR ? `- **When done, create a pull request to the repo's default branch** (main/master) instead of committing directly to dev. Use the /pr skill or gh CLI to open the PR.` : task.metadata?.app && worktreeInfo ? `- Commit code after each feature or bug fix using the git tools or /cam skill. Your worktree branch will be automatically merged back to the default branch when your task completes — do NOT open a PR.` : `- Commit code after each feature or bug fix using the git tools or /cam skill`}+${task.metadata?.app && worktreeInfo && willOpenPR ? `- Commit code after each feature or bug fix using the git tools or /cam skill. A pull request will be automatically created when your task completes — do NOT open a PR manually.` : task.metadata?.app && worktreeInfo ? `- Commit code after each feature or bug fix using the git tools or /cam skill. Your worktree branch will be automatically merged back to the default branch when your task completes — do NOT open a PR.` : `- Commit code after each feature or bug fix using the git tools or /cam skill`}## Git Hygiene (CRITICAL)- **Before starting work**, run \`git status\` to verify a clean working tree. If there are uncommitted changes from a previous agent or manual work, **stash or discard them** before proceeding — do NOT commit someone else's changes.diff --git a/server/services/subAgentSpawner.test.js b/server/services/subAgentSpawner.test.jsindex ecf6d9d3bc..63d3b50005 100644--- a/server/services/subAgentSpawner.test.js+++ b/server/services/subAgentSpawner.test.js@@ -671,4 +671,141 @@ describe('Task Failure Retry Logic', () => {});});+ // --- isTruthyMeta helper (inline copy for testing) ---+ describe('isTruthyMeta', () => {+ const isTruthyMeta = (value) => value === true || value === 'true';++ it('should return true for boolean true', () => {+ expect(isTruthyMeta(true)).toBe(true);+ });++ it('should return true for string "true"', () => {+ expect(isTruthyMeta('true')).toBe(true);+ });++ it('should return false for false', () => {+ expect(isTruthyMeta(false)).toBe(false);+ });++ it('should return false for undefined/null/other values', () => {+ expect(isTruthyMeta(undefined)).toBe(false);+ expect(isTruthyMeta(null)).toBe(false);+ expect(isTruthyMeta('false')).toBe(false);+ expect(isTruthyMeta(1)).toBe(false);+ expect(isTruthyMeta('')).toBe(false);+ });+ });++ // --- openPR worktree decision logic ---+ describe('openPR/useWorktree decision logic', () => {+ function resolveWorktreeFlags(metadata) {+ const isTruthyMeta = (v) => v === true || v === 'true';+ const explicitOpenPR = isTruthyMeta(metadata?.openPR);+ const explicitWorktree = isTruthyMeta(metadata?.useWorktree) || explicitOpenPR;+ return { explicitOpenPR, explicitWorktree };+ }++ it('should not use worktree or PR when neither is set', () => {+ const result = resolveWorktreeFlags({});+ expect(result.explicitWorktree).toBe(false);+ expect(result.explicitOpenPR).toBe(false);+ });++ it('should use worktree but not PR when only useWorktree is set', () => {+ const result = resolveWorktreeFlags({ useWorktree: true });+ expect(result.explicitWorktree).toBe(true);+ expect(result.explicitOpenPR).toBe(false);+ });++ it('should imply worktree when openPR is set', () => {+ const result = resolveWorktreeFlags({ openPR: true });+ expect(result.explicitWorktree).toBe(true);+ expect(result.explicitOpenPR).toBe(true);+ });++ it('should use both when both are set', () => {+ const result = resolveWorktreeFlags({ useWorktree: true, openPR: true });+ expect(result.explicitWorktree).toBe(true);+ expect(result.explicitOpenPR).toBe(true);+ });++ it('should handle string "true" values from form/URL params', () => {+ const result = resolveWorktreeFlags({ useWorktree: 'true', openPR: 'true' });+ expect(result.explicitWorktree).toBe(true);+ expect(result.explicitOpenPR).toBe(true);+ });++ it('should not use worktree when useWorktree is false and openPR is false', () => {+ const result = resolveWorktreeFlags({ useWorktree: false, openPR: false });+ expect(result.explicitWorktree).toBe(false);+ expect(result.explicitOpenPR).toBe(false);+ });+ });++ // --- applyAppWorktreeDefault logic ---+ describe('applyAppWorktreeDefault', () => {+ function applyAppWorktreeDefault(metadata, app) {+ if (metadata.useWorktree === undefined) {+ if (app.defaultUseWorktree === true) {+ metadata.useWorktree = true;+ } else if (app.defaultUseWorktree === false) {+ metadata.useWorktree = false;+ }+ }+ if (metadata.openPR === undefined && metadata.useWorktree !== false) {+ if (app.defaultOpenPR === true) {+ metadata.openPR = true;+ metadata.useWorktree = true;+ } else if (app.defaultOpenPR === false) {+ metadata.openPR = false;+ }+ }+ if (metadata.useWorktree === false) {+ metadata.openPR = false;+ }+ }++ it('should fill defaults when metadata has no worktree/openPR fields', () => {+ const metadata = {};+ applyAppWorktreeDefault(metadata, { defaultUseWorktree: true, defaultOpenPR: true });+ expect(metadata.useWorktree).toBe(true);+ expect(metadata.openPR).toBe(true);+ });++ it('should not override task-type metadata that is already set', () => {+ const metadata = { useWorktree: false, openPR: false };+ applyAppWorktreeDefault(metadata, { defaultUseWorktree: true, defaultOpenPR: true });+ expect(metadata.useWorktree).toBe(false);+ expect(metadata.openPR).toBe(false);+ });++ it('should enforce openPR=false when useWorktree=false', () => {+ const metadata = { useWorktree: false };+ applyAppWorktreeDefault(metadata, { defaultOpenPR: true });+ expect(metadata.openPR).toBe(false);+ });++ it('should imply useWorktree when defaultOpenPR is true', () => {+ const metadata = {};+ applyAppWorktreeDefault(metadata, { defaultOpenPR: true });+ expect(metadata.useWorktree).toBe(true);+ expect(metadata.openPR).toBe(true);+ });++ it('should apply defaultUseWorktree without affecting already-set openPR', () => {+ const metadata = { openPR: true };+ applyAppWorktreeDefault(metadata, { defaultUseWorktree: false });+ // useWorktree was unset, gets filled with false; invariant enforces openPR=false+ expect(metadata.useWorktree).toBe(false);+ expect(metadata.openPR).toBe(false);+ });++ it('should leave metadata unchanged when app has no defaults', () => {+ const metadata = { useWorktree: true, openPR: true };+ applyAppWorktreeDefault(metadata, {});+ expect(metadata.useWorktree).toBe(true);+ expect(metadata.openPR).toBe(true);+ });+ });+});