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): settle merged review outcomes during restart recovery

[._.]/ Adam Eivyb7693b2f5106
diff --git a/server/services/agentFinalization.js b/server/services/agentFinalization.js
index 02a993a53e..8cae8608cc 100644
--- a/server/services/agentFinalization.js
+++ b/server/services/agentFinalization.js
@@ -1175,6 +1175,21 @@ export function dispatchRecoveredTaskOutputHook({ agentId, task, success, worksp
});
}
+/** Server-owned merge evidence settles only the assigned review/merge target. */
+async function completionReviewFollowUp({ task, agentId, workspacePath, reportedOutcome, eligible }) {
+ if (!eligible) return null;
+ const verified = await (await import('./reviewFollowUpCompletion.js')).verifyMergedReviewFollowUp(task, workspacePath)
+ .catch(() => null);
+ if (!verified) return null;
+ const taskOutcome = {
+ status: reportedOutcome?.status === 'completed' ? 'completed' : 'no-action-needed',
+ reason: `The assigned review/merge target is verified MERGED: ${verified.prUrl}. Preserved corrections and checkout retirement remain separate obligations.`,
+ };
+ await updateAgent(agentId, { metadata: { taskOutcome, taskOutcomeSource: 'forge-verification',
+ reportedTaskOutcome: reportedOutcome, mergedReviewFollowUp: verified } });
+ return { ...verified, taskOutcome };
+}
+
async function completionClaimDisposition({ task, agent, agentId, reportedOutcome, success }) {
if (!success || reportedOutcome?.status !== 'no-action-needed' || !hasClaimFlowContract(task)) return null;
return import('./claimDisposition.js').then(async ({ verifyClaimDisposition }) =>
@@ -1228,12 +1243,16 @@ export async function retireDeadAgent({ agent, task, success, exitCode, duration
const isPrivateSecurity = isPrivateSecurityTask(task) || isPrivateSecurityTask(agent);
const interruption = agent.metadata?.executionInterruption;
const refused = ['content-refusal', 'content-filtered'].includes(interruption?.category);
- const reportedOutcome = !isPrivateSecurity && !refused
+ let reportedOutcome = !isPrivateSecurity && !refused
? await persistCompletionSummary(agent.id, { workspacePath: agent.metadata?.workspacePath, existingRecord: agent }) : null;
+ const executionSuccess = success && exitCode === 0;
+ const mergedReviewFollowUp = await completionReviewFollowUp({ task, agentId: agent.id,
+ workspacePath: agent.metadata?.workspacePath, reportedOutcome,
+ eligible: executionSuccess && !isPrivateSecurity && !interruption });
+ if (mergedReviewFollowUp) reportedOutcome = mergedReviewFollowUp.taskOutcome;
// Only an explicit hold suppresses orphan recovery; a failed delivery check
// must retain the caller's normal failure/retry handling.
const deliveryHeld = ['blocked', 'incomplete'].includes(reportedOutcome?.status);
- const executionSuccess = success && exitCode === 0;
let effectiveSuccess = isPrivateSecurity || refused || deliveryHeld ? false : success;
const errorAnalysis = refused ? {
category: interruption.category, origin: interruption.source || 'provider',
@@ -1255,8 +1274,8 @@ export async function retireDeadAgent({ agent, task, success, exitCode, duration
: agent.result?.validationPassed ?? null;
// A disposition never erases a recorded failed delivery gate. A saved pass,
// however, cannot replace a fresh selected-issue proof on no-op recovery.
- const validationPassed = claimDisposition && recoveredValidation !== false
- ? claimDisposition.status === 'verified' ? true : null
+ const validationPassed = recoveredValidation !== false && (mergedReviewFollowUp || claimDisposition)
+ ? mergedReviewFollowUp || claimDisposition.status === 'verified' ? true : null
: recoveredValidation;
const taskOutcome = reportedOutcome ? resolveTaskOutcome({ reported: reportedOutcome,
success: effectiveSuccess, validationPassed }) : null;
@@ -1716,22 +1735,12 @@ export async function finalizeAgent({
const executionSuccess = reportedSuccess && (typeof exitCode !== 'number' || exitCode === 0);
let reportedTaskOutcome = !terminatedByUser && !isPrivateSecurityTask(task)
? await persistCompletionSummary(agentId, { workspacePath, finalSummary }) : null;
- // This task owns one assigned PR, not a new delivery of local corrections.
- // A successful process may report a hold after discovering that PR merged.
- // Verify the exact trusted target before that hold can schedule another run.
- const mergedReviewFollowUp = executionSuccess && !terminatedByUser && !executionInterruption
- && !isPrivateSecurityTask(task) && !reportedErrorAnalysis?.actionable && !reportedErrorAnalysis?.permanent
- ? await (await import('./reviewFollowUpCompletion.js')).verifyMergedReviewFollowUp(task, workspacePath)
- .catch(() => null) : null;
- if (mergedReviewFollowUp) {
- const originalOutcome = reportedTaskOutcome;
- reportedTaskOutcome = {
- status: originalOutcome?.status === 'completed' ? 'completed' : 'no-action-needed',
- reason: `The assigned review/merge target is verified MERGED: ${mergedReviewFollowUp.prUrl}. Preserved corrections and checkout retirement remain separate obligations.`,
- };
- await updateAgent(agentId, { metadata: { taskOutcome: reportedTaskOutcome, taskOutcomeSource: 'forge-verification',
- reportedTaskOutcome: originalOutcome, mergedReviewFollowUp } });
- }
+ // A successful process may report a hold after discovering its PR merged.
+ // Verify that exact target before the hold can schedule another run.
+ const mergedReviewFollowUp = await completionReviewFollowUp({ task, agentId, workspacePath, reportedOutcome: reportedTaskOutcome,
+ eligible: executionSuccess && !terminatedByUser && !executionInterruption
+ && !isPrivateSecurityTask(task) && !reportedErrorAnalysis?.actionable && !reportedErrorAnalysis?.permanent });
+ if (mergedReviewFollowUp) reportedTaskOutcome = mergedReviewFollowUp.taskOutcome;
const deliveryHeld = ['blocked', 'incomplete'].includes(reportedTaskOutcome?.status);
if (deliveryHeld && reportedTaskOutcome.recovery?.mode !== 'cleanup'
&& !reportedErrorAnalysis?.actionable && !reportedErrorAnalysis?.permanent) {
diff --git a/server/services/agentFinalization.outputHooks.test.js b/server/services/agentFinalization.outputHooks.test.js
index 588ee2b596..67573534d4 100644
--- a/server/services/agentFinalization.outputHooks.test.js
+++ b/server/services/agentFinalization.outputHooks.test.js
@@ -414,25 +414,31 @@ describe('recovery output-hook dispatch (#3182)', () => {
reviewLoopPRBranch: 'fix/example', failureCount: 2,
} });
- it.each(['blocked', 'incomplete', 'no-action-needed'])('completes a verified merged review target reported as %s without retrying', async status => {
+ it.each(['normal', 'restart'])('completes a verified merged review target without retrying during %s completion', async mode => {
isProgrammaticIoTaskType.mockReturnValue(false);
mergedTarget.getOriginInfo.mockResolvedValue({ host: 'github.com', fullName: 'atomantic/PortOS' });
mergedTarget.probeChangeRequestState.mockResolvedValue({ readable: true, prState: 'MERGED' });
const task = reviewTask();
- const reported = { status, reason: 'The target is merged; corrections are tracked separately and the checkout is preserved.' };
- const result = await finalizeAgent({ agentId: 'agent-example', runId: 'run-example', task, success: true,
- exitCode: 0, duration: 1000, workspacePath: '/example/repo', outputBuffer: '',
- finalSummary: JSON.stringify({ summary: reported.reason, taskOutcome: reported }) });
+ const reported = { status: mode === 'normal' ? 'blocked' : 'incomplete', reason: 'The target is merged; corrections are tracked separately and the checkout is preserved.' };
+ persistedAgent.metadata.workspacePath = '/example/repo';
+ persistedAgent.metadata.taskOutcome = reported;
+ persistedAgent.metadata.taskOutcomeSource = 'terminal-result';
+ const result = mode === 'normal'
+ ? await finalizeAgent({ agentId: 'agent-example', runId: 'run-example', task, success: true,
+ exitCode: 0, duration: 1000, workspacePath: '/example/repo', outputBuffer: '',
+ finalSummary: JSON.stringify({ summary: reported.reason, taskOutcome: reported }) })
+ : await retireDeadAgent({ agent: persistedAgent, task, success: true, exitCode: 0, duration: 1000 });
expect(mergedTarget.probeChangeRequestState).toHaveBeenCalledWith('/example/repo', { number: 123, branch: 'fix/example' });
expect(result).toMatchObject({ success: true, taskOutcome: { status: 'no-action-needed' } });
- expect(updateTask).toHaveBeenCalledWith(task.id, expect.objectContaining({ status: 'completed',
+ if (mode === 'normal') expect(updateTask).toHaveBeenCalledWith(task.id, expect.objectContaining({ status: 'completed',
metadata: expect.objectContaining({ failureCount: 2, taskOutcome: expect.objectContaining({ status: 'no-action-needed' }) }) }), 'internal');
expect(completeAgent).toHaveBeenCalledWith('agent-example', expect.objectContaining({ success: true, validationPassed: true,
taskOutcome: expect.objectContaining({ status: 'no-action-needed' }) }));
expect(persistedAgent.metadata).toMatchObject({ taskOutcomeSource: 'forge-verification', reportedTaskOutcome: reported });
expect(resolveFailedTaskUpdate).not.toHaveBeenCalled();
expect(addTask).not.toHaveBeenCalled();
- expect(processAgentCompletion).toHaveBeenCalledWith('agent-example', task, true, '');
+ if (mode === 'normal') expect(processAgentCompletion).toHaveBeenCalledWith('agent-example', task, true, '');
+ else expect(result.deliveryHeld).toBe(false);
});
it.each([