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

test: drive real prepareAgentSpawn/prepareAgentWorkspace decisions instead of copied logic (#11883)

Adam Eivyc95d54869b63
diff --git a/server/services/agentLifecycle.test.js b/server/services/agentLifecycle.test.js
index 5b2a319cab..99e77159d7 100644
--- a/server/services/agentLifecycle.test.js
+++ b/server/services/agentLifecycle.test.js
@@ -31,7 +31,6 @@ import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { spawningTasks, runnerAgents } from './agentState.js';
import { withSpawnDedupGuard, withMapEntryCleanup, withUpdateInProgressGuard, SPAWN_DEDUP_SKIP, SPAWN_UPDATE_SKIP } from './agentGuards.js';
-import { isInternalTaskId } from '../lib/taskParser.js';
import { PROVIDER_CONFIG_BLOCKED_CATEGORY, PAUSED_BLOCKED_CATEGORIES, USER_DECISION_BLOCKED_CATEGORIES } from '../lib/taskBlockCategories.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -899,64 +898,7 @@ describe('agentLifecycle — runner OpenCode Ollama env (#2243 / #2190)', () =>
});
});
-// ─── taskType normalization for direct task:ready emits (issue #2633) ────────
-//
-// Direct `task:ready` emitters (Creative Director bridge, dequeueNextTask,
-// spawnPriority0OnDemand) publish task records with no `taskType`. Every
-// claim/in_progress `updateTask` in runAgentSpawn falls back to
-// `task.taskType || 'user'`, so an internal (`sys-*`) task without taskType
-// would write to TASKS.md, miss the record, and return a truthy `{ error }`
-// object the `if (!updateResult)` check silently swallowed. The fix normalizes
-// taskType at the top of runAgentSpawn via the real isInternalTaskId classifier.
-describe('taskType normalization — behavior (issue #2633)', () => {
- // Mirrors the normalization at the top of runAgentSpawn against the REAL
- // isInternalTaskId import, so a change to the internal-prefix list stays in
- // sync with what the spawn path routes on (inline-pure-logic pattern).
- const normalizeTaskType = (task) => {
- if (task && !task.taskType) {
- task.taskType = isInternalTaskId(task.id || '') ? 'internal' : 'user';
- }
- return task;
- };
-
- it('routes a sys-* id with no taskType to the internal file (COS-TASKS.md)', () => {
- const task = { id: 'sys-002', description: 'internal task' };
- normalizeTaskType(task);
- expect(task.taskType, "sys-* must resolve to 'internal' so updateTask targets COS-TASKS.md").toBe('internal');
- });
-
- it('routes cd-* and app-improve-* ids (other internal prefixes) to internal', () => {
- expect(normalizeTaskType({ id: 'cd-42' }).taskType).toBe('internal');
- expect(normalizeTaskType({ id: 'app-improve-9' }).taskType).toBe('internal');
- });
-
- it('leaves a user task-* id defaulting to user (unchanged spawn behavior)', () => {
- const task = { id: 'task-abc' };
- normalizeTaskType(task);
- expect(task.taskType).toBe('user');
- });
-
- it('preserves an already-present taskType (does not reclassify an explicit user task)', () => {
- const task = { id: 'sys-003', taskType: 'user' };
- normalizeTaskType(task);
- expect(task.taskType, 'an explicit taskType must win over id-based inference').toBe('user');
- });
-
- it('defaults a missing id to user rather than throwing', () => {
- expect(normalizeTaskType({}).taskType).toBe('user');
- });
-});
-
-describe('runAgentSpawn source — taskType normalization + claim-miss guard (issue #2633)', () => {
- it('normalizes taskType at the top, BEFORE the first claim updateTask', () => {
- const normalizeIdx = RUN_SPAWN_BODY.indexOf('isInternalTaskId(task.id');
- const firstUpdateIdx = RUN_SPAWN_BODY.indexOf('await updateTask(task.id');
- expect(normalizeIdx, 'runAgentSpawn must derive taskType from the id via isInternalTaskId').toBeGreaterThan(-1);
- expect(firstUpdateIdx, 'runAgentSpawn must call updateTask').toBeGreaterThan(-1);
- expect(normalizeIdx, 'taskType must be normalized BEFORE any updateTask write so every claim routes to the right file')
- .toBeLessThan(firstUpdateIdx);
- });
-
+describe('runAgentSpawn source — claim-miss guard (issue #2633)', () => {
it('only a null in_progress result is fatal — an { error } miss must NOT block the spawn', () => {
// A truthy `{ error }` is EXPECTED for legitimately-unpersisted autonomous
// emits (Priority 3 idle-review tasks carry taskType:'internal' but are
diff --git a/server/services/agentWorkspacePrep.test.js b/server/services/agentWorkspacePrep.test.js
index cad09a3e79..2c79e959ee 100644
--- a/server/services/agentWorkspacePrep.test.js
+++ b/server/services/agentWorkspacePrep.test.js
@@ -163,6 +163,35 @@ describe('prepareAgentWorkspace', () => {
expect(createWorktree).toHaveBeenCalled();
});
+ describe('worktree isolation flags', () => {
+ const run = async (metadata) => {
+ ensureLatest.mockResolvedValue({ success: true, upToDate: true });
+ createWorktree.mockResolvedValue({ worktreePath: '/mock/worktrees/agent-flags', branchName: 'cos/t-flags/agent-flags', baseBranch: 'main' });
+ return prepareAgentWorkspace({ agentId: 'agent-flags', task: { id: 't-flags', taskType: 'user', metadata } });
+ };
+
+ it('openPR alone implies an isolated worktree and never pulls the shared checkout', async () => {
+ const r = await run({ openPR: true });
+ expect(r.explicitWorktree).toBe(true);
+ expect(createWorktree).toHaveBeenCalled();
+ expect(ensureLatest).not.toHaveBeenCalled();
+ });
+
+ it('useWorktree alone isolates without implying a PR', async () => {
+ const r = await run({ useWorktree: 'true' });
+ expect(r.explicitWorktree).toBe(true);
+ expect(createWorktree).toHaveBeenCalled();
+ expect(ensureLatest).not.toHaveBeenCalled();
+ });
+
+ it('neither flag runs in the shared checkout after pulling it', async () => {
+ const r = await run({});
+ expect(r.explicitWorktree).toBe(false);
+ expect(createWorktree).not.toHaveBeenCalled();
+ expect(ensureLatest).toHaveBeenCalled();
+ });
+ });
+
it('read-only task: returns ready with the shared workspace and skips the git pull', async () => {
const task = { id: 't-ro', taskType: 'user', metadata: { readOnly: true } };
const r = await prepareAgentWorkspace({ agentId: 'agent-ro', task });
diff --git a/server/services/cosChallengeOwnership.test.js b/server/services/cosChallengeOwnership.test.js
index 45dd7daf3a..0c9238223c 100644
--- a/server/services/cosChallengeOwnership.test.js
+++ b/server/services/cosChallengeOwnership.test.js
@@ -163,3 +163,27 @@ describe('challenge ownership workflow', () => {
expect(results.map(r => r.status || r.code)).toEqual(['challenged', 'CHALLENGE_EXHAUSTED']);
});
});
+
+// A direct task:ready emit (Creative Director bridge, on-demand) carries no
+// taskType; the claim write must still land in the file that owns the id (#2633).
+describe('prepareAgentSpawn taskType routing', () => {
+ it.each([
+ ['sys-example', 'internal'],
+ ['cd-example', 'internal'],
+ ['task-example', 'user'],
+ ])('claims %s with no taskType in the %s store', async (id, expectedType) => {
+ await addTask({ id, description: 'Example work' }, expectedType);
+ const context = await prepareAgentSpawn({ id, description: 'Example work', metadata: {} });
+ expect(context).not.toBeNull();
+ expect(context.task.taskType).toBe(expectedType);
+ expect(context.claimAcquired).toBe(true);
+ expect((await getTaskById(id)).taskType).toBe(expectedType);
+ });
+
+ it('keeps an explicit taskType over id-based inference', async () => {
+ await addTask({ id: 'sys-explicit', description: 'Example work' }, 'user');
+ const context = await prepareAgentSpawn({ id: 'sys-explicit', taskType: 'user', description: 'Example work', metadata: {} });
+ expect(context.claimAcquired).toBe(true);
+ expect(context.task.taskType).toBe('user');
+ });
+});
diff --git a/server/services/subAgentSpawner.test.js b/server/services/subAgentSpawner.test.js
index 91f0267d40..8efbde1a91 100644
--- a/server/services/subAgentSpawner.test.js
+++ b/server/services/subAgentSpawner.test.js
@@ -127,51 +127,6 @@ describe('Worktree & metadata flag helpers', () => {
});
});
- // --- openPR worktree decision logic (uses imported isTruthyMeta) ---
- describe('openPR/useWorktree decision logic', () => {
- function resolveWorktreeFlags(metadata) {
- 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 (imported from production cos.js) ---
describe('applyAppWorktreeDefault', () => {
it('should fill defaults when metadata has no worktree/openPR fields', () => {