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

feat(cos): programmatic precondition gates for pipeline stages

Adam Eivy1cd3f8bc7c0e
diff --git a/server/services/cos.js b/server/services/cos.js
index daf33ef430..87db5e99b0 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -1934,6 +1934,25 @@ Use model: claude-opus-4-5-20251101 for thorough security analysis`
* Initialize pipeline runtime state on metadata if pipeline stages are configured.
* Mutates the metadata object in place.
*/
+/**
+ * Check if a pipeline stage's precondition is met.
+ * Supports { fileExists: 'path' } and { fileNotExists: 'path' }.
+ * Paths are relative to repoPath.
+ */
+export function checkStagePrecondition(stage, repoPath) {
+ const pre = stage?.precondition;
+ if (!pre || !repoPath) return { passed: true };
+ if (pre.fileExists) {
+ const fullPath = join(repoPath, pre.fileExists);
+ if (!existsSync(fullPath)) return { passed: false, reason: `${pre.fileExists} does not exist` };
+ }
+ if (pre.fileNotExists) {
+ const fullPath = join(repoPath, pre.fileNotExists);
+ if (existsSync(fullPath)) return { passed: false, reason: `${pre.fileNotExists} already exists` };
+ }
+ return { passed: true };
+}
+
function initializePipelineMetadata(metadata) {
if (!metadata.pipeline?.stages?.length) return;
metadata.pipeline = {
@@ -2067,6 +2086,16 @@ async function generateManagedAppImprovementTask(app, state) {
initializePipelineMetadata(metadata);
+ // Check stage 0 precondition before creating the task
+ const stage0 = metadata.pipeline?.stages?.[0];
+ if (stage0?.precondition) {
+ const check = checkStagePrecondition(stage0, app.repoPath);
+ if (!check.passed) {
+ emitLog('info', `⏭️ Skipping ${nextType} for ${app.name}: ${check.reason}`, { appId: app.id, analysisType: nextType });
+ return null;
+ }
+ }
+
const promptTemplate = metadata.pipeline?.stages
? await taskSchedule.getStagePrompt(nextType, 0)
: await taskSchedule.getTaskPrompt(nextType);
@@ -2148,6 +2177,16 @@ async function generateManagedAppImprovementTaskForType(taskType, app, state) {
initializePipelineMetadata(metadata);
+ // Check stage 0 precondition before creating the task
+ const stage0 = metadata.pipeline?.stages?.[0];
+ if (stage0?.precondition) {
+ const check = checkStagePrecondition(stage0, app.repoPath);
+ if (!check.passed) {
+ emitLog('info', `⏭️ Skipping ${taskType} for ${app.name}: ${check.reason}`, { appId: app.id, analysisType: taskType });
+ return null;
+ }
+ }
+
const promptTemplate = metadata.pipeline?.stages
? await taskSchedule.getStagePrompt(taskType, 0)
: await taskSchedule.getTaskPrompt(taskType);
diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.js
index 805cb4b739..1033511953 100644
--- a/server/services/subAgentSpawner.js
+++ b/server/services/subAgentSpawner.js
@@ -1824,6 +1824,20 @@ async function handlePipelineProgression(task, agentId, success) {
}
const nextStage = stages[nextStageIndex];
+
+ // Check next stage's precondition before advancing
+ if (nextStage.precondition && task.metadata.repoPath) {
+ const { checkStagePrecondition } = await import('./cos.js');
+ const check = checkStagePrecondition(nextStage, task.metadata.repoPath);
+ if (!check.passed) {
+ await updateTask(task.id, {
+ metadata: { ...task.metadata, pipeline: { ...pipeline, status: 'failed', stageResults: updatedResults } }
+ }, task.taskType);
+ emitLog('warn', `⏭️ Pipeline ${pipeline.id} stage ${nextStageIndex} precondition failed: ${check.reason}`, { pipelineId: pipeline.id });
+ return;
+ }
+ }
+
const taskScheduleMod = await import('./taskSchedule.js');
let prompt = await taskScheduleMod.getStagePrompt(task.metadata.analysisType, nextStageIndex);
if (task.metadata.appName) prompt = prompt.replace(/\{appName\}/g, task.metadata.appName);
diff --git a/server/services/taskSchedule.js b/server/services/taskSchedule.js
index 4bfc7ab700..152b84c8c7 100644
--- a/server/services/taskSchedule.js
+++ b/server/services/taskSchedule.js
@@ -402,38 +402,31 @@ pick up and implement.
Repository: {repoPath}
-## Phase 1 — Check for Existing Review
+## Phase 1 — Gather Context
-1. Check if REVIEW.md exists in {repoPath}
-2. If REVIEW.md exists, STOP immediately. Output:
- "⏭️ REVIEW.md already exists — skipping review to avoid overwriting pending recommendations."
- A previous review has not been processed yet. Do not overwrite it.
+1. Read GOALS.md (if exists) for project goals and priorities
+2. Read PLAN.md (if exists) to understand already-planned work — do NOT re-suggest items already planned
+3. Read DONE.md (if exists) to understand completed work — do NOT re-suggest items already done
+4. Read REJECTED.md (if exists) to understand previously rejected recommendations — do NOT re-suggest rejected items
+5. Read CLAUDE.md for project conventions and architecture
+6. Review the codebase structure, key files, recent git log (last 20 commits)
-## Phase 2 — Gather Context
-
-3. Read GOALS.md (if exists) for project goals and priorities
-4. Read PLAN.md (if exists) to understand already-planned work — do NOT re-suggest items already planned
-5. Read DONE.md (if exists) to understand completed work — do NOT re-suggest items already done
-6. Read REJECTED.md (if exists) to understand previously rejected recommendations — do NOT re-suggest rejected items
-7. Read CLAUDE.md for project conventions and architecture
-8. Review the codebase structure, key files, recent git log (last 20 commits)
-
-## Phase 3 — Deep Review
+## Phase 2 — Deep Review
Examine the codebase thoroughly across these dimensions. Skip any recommendations that overlap with PLAN.md, DONE.md, or REJECTED.md items:
-9. **Code Quality**: DRY violations, dead code, overly complex functions, missing error handling, inconsistent patterns, tech debt
-10. **Architecture**: Component organization, separation of concerns, data flow issues, coupling problems, missing abstractions (or unnecessary abstractions)
-11. **Features**: Missing capabilities that would make the app more useful, based on GOALS.md priorities and codebase gaps
-12. **UX/Design**: UI inconsistencies, accessibility issues, mobile responsiveness gaps, confusing user flows, missing feedback/loading states
-13. **Performance**: N+1 queries, unnecessary re-renders, large bundle imports, missing caching, slow operations
-14. **Security**: Input validation gaps, injection risks, exposed secrets, unsafe defaults
-15. **Testing**: Missing test coverage, brittle tests, untested edge cases
-16. **Developer Experience**: Missing docs, confusing setup, poor error messages
+7. **Code Quality**: DRY violations, dead code, overly complex functions, missing error handling, inconsistent patterns, tech debt
+8. **Architecture**: Component organization, separation of concerns, data flow issues, coupling problems, missing abstractions (or unnecessary abstractions)
+9. **Features**: Missing capabilities that would make the app more useful, based on GOALS.md priorities and codebase gaps
+10. **UX/Design**: UI inconsistencies, accessibility issues, mobile responsiveness gaps, confusing user flows, missing feedback/loading states
+11. **Performance**: N+1 queries, unnecessary re-renders, large bundle imports, missing caching, slow operations
+12. **Security**: Input validation gaps, injection risks, exposed secrets, unsafe defaults
+13. **Testing**: Missing test coverage, brittle tests, untested edge cases
+14. **Developer Experience**: Missing docs, confusing setup, poor error messages
-## Phase 4 — Write REVIEW.md
+## Phase 3 — Write REVIEW.md
-17. Write findings to REVIEW.md in {repoPath} using this format:
+15. Write findings to REVIEW.md in {repoPath} using this format:
\\\`\\\`\\\`markdown
# Code Review — {appName}
@@ -453,7 +446,7 @@ Generated: <today's date>
Order recommendations by priority (HIGH first), then by effort (Small first).
-18. Do NOT implement any changes — this is a review-only stage`,
+16. Do NOT implement any changes — this is a review-only stage`,
'code-reviewer-implement': `[Review: {appName}] Triage & Implement Review (Stage 2)
@@ -464,11 +457,10 @@ Repository: {repoPath}
## Phase 1 — Read Context
1. Read REVIEW.md from {repoPath} — this contains the recommendations from Stage 1
-2. If REVIEW.md does not exist, STOP — output: "No REVIEW.md found, nothing to triage."
-3. Read GOALS.md (if exists) for alignment context
-4. Read PLAN.md (if exists) for current planned work
-5. Read DONE.md (if exists) for completed work
-6. Read CLAUDE.md for project conventions
+2. Read GOALS.md (if exists) for alignment context
+3. Read PLAN.md (if exists) for current planned work
+4. Read DONE.md (if exists) for completed work
+5. Read CLAUDE.md for project conventions
## Phase 2 — Triage Each Recommendation
@@ -504,11 +496,10 @@ Categorize into:
## Phase 5 — Cleanup
12. Delete REVIEW.md from {repoPath} — all items have been triaged
-13. Commit: "chore: remove processed REVIEW.md"
## Phase 6 — Report
-14. Summarize:
+13. Summarize:
- Recommendations implemented (with brief descriptions)
- Items added to PLAN.md
- Items rejected (with reasons)
@@ -1208,7 +1199,7 @@ export const SELF_IMPROVEMENT_TASK_TYPES = [
];
// Shared config for code-reviewer-a and code-reviewer-b (two instances for independent provider/model configuration)
-const CODE_REVIEWER_INTERVAL = { type: INTERVAL_TYPES.WEEKLY, enabled: false, weekdaysOnly: true, providerId: null, model: null, prompt: null, taskMetadata: { useWorktree: true, openPR: true, simplify: true, pipeline: { stages: [{ name: 'Codebase Review', promptKey: 'code-reviewer-review', readOnly: true, providerId: null, model: null }, { name: 'Triage & Implement', promptKey: 'code-reviewer-implement', readOnly: false, providerId: null, model: null }] } } };
+const CODE_REVIEWER_INTERVAL = { type: INTERVAL_TYPES.WEEKLY, enabled: false, weekdaysOnly: true, providerId: null, model: null, prompt: null, taskMetadata: { useWorktree: true, openPR: true, simplify: true, pipeline: { stages: [{ name: 'Codebase Review', promptKey: 'code-reviewer-review', readOnly: true, providerId: null, model: null, precondition: { fileNotExists: 'REVIEW.md' } }, { name: 'Triage & Implement', promptKey: 'code-reviewer-implement', readOnly: false, providerId: null, model: null, precondition: { fileExists: 'REVIEW.md' } }] } } };
const DEFAULT_TASK_INTERVALS = {
'security': { type: INTERVAL_TYPES.WEEKLY, enabled: false, providerId: null, model: null, prompt: null },