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: separate agent completion follow-up steps

Adam Eivyc05bd4248556
diff --git a/server/services/agentFinalization.js b/server/services/agentFinalization.js
index 9cfbc4418e..2139bcb521 100644
--- a/server/services/agentFinalization.js
+++ b/server/services/agentFinalization.js
@@ -1321,6 +1321,125 @@ function resolvePrEvidence({ primaryObservation, noChangeObservation }) {
};
}
+async function followUpAgentGoalFidelity({ agentId, task, fidelity, fidelityDowngrade }) {
+ // A finding that lives only in this run's record dies with the agent card
+ // nobody opened. When the user has configured it, the follow-up files the
+ // finding on the project's own tracker and/or queues the run that fixes it.
+ // Lazily imported: the filer reaches the apps roster, both forge CLIs, and
+ // (for a JIRA-tracked app) the JIRA client, and a static edge would put all
+ // of it on the closure of every suite that reaches finalization. Gated on the
+ // WIDEST trigger so a clean `ship` — the overwhelmingly common verdict — does
+ // not evaluate that graph just to be told there is nothing to act on.
+ if (resolvePrCompletion(task?.metadata) !== PR_COMPLETIONS.LEAVE_OPEN && goalFidelityFollowUpApplies(fidelity.review, 'any-finding')) {
+ const followUp = await import('./goalFidelityFollowUp.js')
+ .then(({ runGoalFidelityFollowUp }) => runGoalFidelityFollowUp({ agentId, task, review: fidelity.review, context: fidelity.context }))
+ .catch(err => {
+ emitLog('warn', `Goal-fidelity follow-up failed for ${agentId}: ${err.message}`, { agentId, taskId: task?.id });
+ return null;
+ });
+ if (followUp?.ran) {
+ emitLog('info', `📌 ${formatGoalFidelityFollowUpSummary(followUp)} for ${agentId}`, { agentId, taskId: task?.id });
+ // Recorded on the review so the agent card can link the issue and the
+ // queued task without re-deriving either from the tracker.
+ fidelity.review.followUp = {
+ ...(followUp.issue ? { issue: { number: followUp.issue.number, url: followUp.issue.url, duplicate: followUp.issue.duplicate === true } } : {}),
+ ...(followUp.issueError ? { issueError: followUp.issueError } : {}),
+ // The producer's verdict rides with the id: a task the loop policy HELD
+ // is not a queued one, and the card must not offer the manual fallback
+ // for a finding that already has a task waiting on the user.
+ ...(followUp.task?.id ? {
+ taskId: followUp.task.id,
+ ...(followUp.task.approvalRequired ? { taskApprovalRequired: true } : {}),
+ ...(followUp.task.duplicate ? { taskDuplicate: true } : {}),
+ } : {}),
+ ...(followUp.taskError ? { taskError: followUp.taskError } : {}),
+ };
+ }
+ }
+
+ if (fidelityDowngrade && !fidelity.review.followUp?.taskId) {
+ // The Review Hub bridges this into a review alert when no follow-up task
+ // exists. A queued (or deduplicated) CoS investigation already carries the
+ // same finding, including when it is waiting for approval, so a second
+ // dismissible alert only asks the user to triage the same work twice.
+ cosEvents.emit(GOAL_FIDELITY_HOLD_EVENT, { agentId, taskId: task?.id, review: fidelity.review });
+ }
+}
+
+async function sidelineFailedAgentProvider({ providerId, verdict, terminatedByUser }) {
+ if (!verdict.success && !terminatedByUser && verdict.errorAnalysis) {
+ // Bench the provider when the PROVIDER is what failed — not the agent's
+ // work. `origin: 'provider'` is agentErrorAnalysis's provenance flag (#2642),
+ // set only for structured provider chrome, never for a loose keyword sweep of
+ // a transcript the agent itself wrote. Without a bench the provider stays
+ // "available", so the very next dequeue picks it again and dies identically:
+ // that is how one `agy` account-verification block could take down a whole
+ // queue of tasks in under a minute. `resolveProviderBench` (shared with
+ // promptRunner) picks the window and declines to bench request-specific
+ // failures; the cooldown then routes the retry to a fallback
+ // (agentProviderResolution.js) and auto-expires without anyone intervening.
+ // Provenance is the WHOLE gate — deliberately not `|| category === 'usage-limit'
+ // || category === 'rate-limit'`, which is what this used to key on. Those
+ // categories have loose alternatives (a bare "rate limit" / "quota exceeded" an
+ // agent's own failing test can print), so a category-only check benches a
+ // healthy provider off the agent's transcript. Genuine limits already carry
+ // `origin: 'provider'` via their `structuredMarker`, so nothing real is lost.
+ // Every finalizeAgent caller analyzes through analyzeAgentFailure or
+ // detectImmediateFallbackSignal, and both stamp an origin on every branch.
+ const bench = verdict.errorAnalysis.origin === 'provider' ? resolveProviderBench(verdict.errorAnalysis) : null;
+ // Lazy provider lookup — resolve the active provider only when a marker
+ // fires AND the caller didn't already know the id, keeping the ordinary
+ // failure path free of a settings-file read.
+ const markerProviderId = bench ? providerId || (await getActiveProvider())?.id : null;
+ if (markerProviderId) {
+ // `markUsageLimit` parses its own window out of the provider's message
+ // ("resets 5pm"), so a usage limit keeps its dedicated marker.
+ const mark = bench.marker === 'usage-limit'
+ ? markProviderUsageLimit(markerProviderId, verdict.errorAnalysis)
+ : markProviderUnavailable(markerProviderId, {
+ reason: bench.category,
+ message: bench.message || 'Provider unavailable',
+ waitTimeMs: bench.waitTimeMs
+ });
+ await mark.catch(err => {
+ emitLog('warn', `Failed to sideline provider: ${err.message}`, { providerId: markerProviderId, category: bench.category });
+ });
+ }
+ }
+}
+
+async function recordScheduledAgentOutcome({ agentId, task, verdict, terminatedByUser, hookResult }) {
+ // Type-level consecutive-failure ledger (#2616): feed the per-type
+ // backoff/auto-park in taskSchedule. Only SCHEDULED task types carry
+ // `metadata.analysisType`; user/ad-hoc tasks don't participate — so this gate
+ // deliberately does NOT use resolveTaskHookType (#2727). That resolver falls back
+ // to `task.taskType`, which for an ad-hoc task is the CoS queue category
+ // ('internal', 'user'); ledgering those would invent a failure ledger for a
+ // "task type" that no schedule owns. "Which tasks run a hook" and "which task
+ // types back off" are genuinely different questions. The pure
+ // resolveTypeFailureSignal decides success vs failure vs skip — including the
+ // exit-0-but-unparseable-output case that must count as a failure.
+ const scheduledType = task?.metadata?.analysisType || null;
+ if (scheduledType) {
+ const signal = resolveTypeFailureSignal({
+ success: verdict.success,
+ terminatedByUser,
+ hookResult,
+ errorCategory: verdict.errorAnalysis?.category
+ });
+ if (signal.record !== 'skip') {
+ const ledgerAppId = task?.metadata?.app || null;
+ const { recordTaskTypeFailure, recordTaskTypeSuccess } = await import('./taskSchedule.js');
+ const ledgerUpdate = signal.record === 'failure'
+ ? recordTaskTypeFailure(scheduledType, ledgerAppId, { errorCategory: signal.category })
+ : recordTaskTypeSuccess(scheduledType, ledgerAppId);
+ await ledgerUpdate.catch(err => {
+ emitLog('warn', `⚠️ Task-type ledger update failed for ${scheduledType}: ${err.message}`, { taskType: scheduledType, agentId });
+ });
+ }
+ }
+}
+
/**
* Shared end-of-run state writes for all three spawn paths
* (`handleAgentCompletion` runner-mode, TUI `finish`, direct-CLI `close`).
@@ -1534,48 +1653,7 @@ export async function finalizeAgent({
emitLog('warn', `Goal-fidelity review returned no verdict for ${agentId}: ${fidelity.error}`, { agentId, taskId: task?.id });
}
const judgedVerdict = resolveAgentFinalVerdict({ ...verdictEvidence, fidelityReview: fidelity.review });
- // A finding that lives only in this run's record dies with the agent card
- // nobody opened. When the user has configured it, the follow-up files the
- // finding on the project's own tracker and/or queues the run that fixes it.
- // Lazily imported: the filer reaches the apps roster, both forge CLIs, and
- // (for a JIRA-tracked app) the JIRA client, and a static edge would put all
- // of it on the closure of every suite that reaches finalization. Gated on the
- // WIDEST trigger so a clean `ship` — the overwhelmingly common verdict — does
- // not evaluate that graph just to be told there is nothing to act on.
- if (resolvePrCompletion(task?.metadata) !== PR_COMPLETIONS.LEAVE_OPEN && goalFidelityFollowUpApplies(fidelity.review, 'any-finding')) {
- const followUp = await import('./goalFidelityFollowUp.js')
- .then(({ runGoalFidelityFollowUp }) => runGoalFidelityFollowUp({ agentId, task, review: fidelity.review, context: fidelity.context }))
- .catch(err => {
- emitLog('warn', `Goal-fidelity follow-up failed for ${agentId}: ${err.message}`, { agentId, taskId: task?.id });
- return null;
- });
- if (followUp?.ran) {
- emitLog('info', `📌 ${formatGoalFidelityFollowUpSummary(followUp)} for ${agentId}`, { agentId, taskId: task?.id });
- // Recorded on the review so the agent card can link the issue and the
- // queued task without re-deriving either from the tracker.
- fidelity.review.followUp = {
- ...(followUp.issue ? { issue: { number: followUp.issue.number, url: followUp.issue.url, duplicate: followUp.issue.duplicate === true } } : {}),
- ...(followUp.issueError ? { issueError: followUp.issueError } : {}),
- // The producer's verdict rides with the id: a task the loop policy HELD
- // is not a queued one, and the card must not offer the manual fallback
- // for a finding that already has a task waiting on the user.
- ...(followUp.task?.id ? {
- taskId: followUp.task.id,
- ...(followUp.task.approvalRequired ? { taskApprovalRequired: true } : {}),
- ...(followUp.task.duplicate ? { taskDuplicate: true } : {}),
- } : {}),
- ...(followUp.taskError ? { taskError: followUp.taskError } : {}),
- };
- }
- }
-
- if (fidelityDowngrade && !fidelity.review.followUp?.taskId) {
- // The Review Hub bridges this into a review alert when no follow-up task
- // exists. A queued (or deduplicated) CoS investigation already carries the
- // same finding, including when it is waiting for approval, so a second
- // dismissible alert only asks the user to triage the same work twice.
- cosEvents.emit(GOAL_FIDELITY_HOLD_EVENT, { agentId, taskId: task?.id, review: fidelity.review });
- }
+ await followUpAgentGoalFidelity({ agentId, task, fidelity, fidelityDowngrade });
if (!terminatedByUser && !isPrivateSecurityTask(task)) {
await persistCompletionSummary(agentId, { workspacePath, finalSummary });
@@ -1767,75 +1845,9 @@ export async function finalizeAgent({
emitLog('warn', `⚠️ Failed to update ${label} task ${task.id}: ${taskResult.error} (taskType=${taskType})`, { taskId: task.id, agentId, error: taskResult.error });
}
- if (!verdict.success && !terminatedByUser && verdict.errorAnalysis) {
- // Bench the provider when the PROVIDER is what failed — not the agent's
- // work. `origin: 'provider'` is agentErrorAnalysis's provenance flag (#2642),
- // set only for structured provider chrome, never for a loose keyword sweep of
- // a transcript the agent itself wrote. Without a bench the provider stays
- // "available", so the very next dequeue picks it again and dies identically:
- // that is how one `agy` account-verification block could take down a whole
- // queue of tasks in under a minute. `resolveProviderBench` (shared with
- // promptRunner) picks the window and declines to bench request-specific
- // failures; the cooldown then routes the retry to a fallback
- // (agentProviderResolution.js) and auto-expires without anyone intervening.
- // Provenance is the WHOLE gate — deliberately not `|| category === 'usage-limit'
- // || category === 'rate-limit'`, which is what this used to key on. Those
- // categories have loose alternatives (a bare "rate limit" / "quota exceeded" an
- // agent's own failing test can print), so a category-only check benches a
- // healthy provider off the agent's transcript. Genuine limits already carry
- // `origin: 'provider'` via their `structuredMarker`, so nothing real is lost.
- // Every finalizeAgent caller analyzes through analyzeAgentFailure or
- // detectImmediateFallbackSignal, and both stamp an origin on every branch.
- const bench = verdict.errorAnalysis.origin === 'provider' ? resolveProviderBench(verdict.errorAnalysis) : null;
- // Lazy provider lookup — resolve the active provider only when a marker
- // fires AND the caller didn't already know the id, keeping the ordinary
- // failure path free of a settings-file read.
- const markerProviderId = bench ? providerId || (await getActiveProvider())?.id : null;
- if (markerProviderId) {
- // `markUsageLimit` parses its own window out of the provider's message
- // ("resets 5pm"), so a usage limit keeps its dedicated marker.
- const mark = bench.marker === 'usage-limit'
- ? markProviderUsageLimit(markerProviderId, verdict.errorAnalysis)
- : markProviderUnavailable(markerProviderId, {
- reason: bench.category,
- message: bench.message || 'Provider unavailable',
- waitTimeMs: bench.waitTimeMs
- });
- await mark.catch(err => {
- emitLog('warn', `Failed to sideline provider: ${err.message}`, { providerId: markerProviderId, category: bench.category });
- });
- }
- }
+ await sidelineFailedAgentProvider({ providerId, verdict, terminatedByUser });
- // Type-level consecutive-failure ledger (#2616): feed the per-type
- // backoff/auto-park in taskSchedule. Only SCHEDULED task types carry
- // `metadata.analysisType`; user/ad-hoc tasks don't participate — so this gate
- // deliberately does NOT use resolveTaskHookType (#2727). That resolver falls back
- // to `task.taskType`, which for an ad-hoc task is the CoS queue category
- // ('internal', 'user'); ledgering those would invent a failure ledger for a
- // "task type" that no schedule owns. "Which tasks run a hook" and "which task
- // types back off" are genuinely different questions. The pure
- // resolveTypeFailureSignal decides success vs failure vs skip — including the
- // exit-0-but-unparseable-output case that must count as a failure.
- const scheduledType = task?.metadata?.analysisType || null;
- if (scheduledType) {
- const signal = resolveTypeFailureSignal({
- success: verdict.success,
- terminatedByUser,
- hookResult,
- errorCategory: verdict.errorAnalysis?.category
- });
- if (signal.record !== 'skip') {
- const ledgerAppId = task?.metadata?.app || null;
- const { recordTaskTypeFailure, recordTaskTypeSuccess } = await import('./taskSchedule.js');
- const ledgerUpdate = signal.record === 'failure'
- ? recordTaskTypeFailure(scheduledType, ledgerAppId, { errorCategory: signal.category })
- : recordTaskTypeSuccess(scheduledType, ledgerAppId);
- await ledgerUpdate.catch(err => {
- emitLog('warn', `⚠️ Task-type ledger update failed for ${scheduledType}: ${err.message}`, { taskType: scheduledType, agentId });
- });
- }
- }
+ await recordScheduledAgentOutcome({ agentId, task, verdict, terminatedByUser, hookResult });
await processAgentCompletion(agentId, task, verdict.success, outputBuffer);
diff --git a/server/services/agentFinalization.outputHooks.test.js b/server/services/agentFinalization.outputHooks.test.js
index 2cdae7a91e..82fa5b306e 100644
--- a/server/services/agentFinalization.outputHooks.test.js
+++ b/server/services/agentFinalization.outputHooks.test.js
@@ -78,6 +78,7 @@ vi.mock('./taskSchedule.js', () => ({
}));
import { updateTask, addTask } from './cos.js';
import { completeAgentRun } from './agentRunTracking.js';
+import { processAgentCompletion } from './agentCompletion.js';
import { MAX_TASK_RETRIES, resolveFailedTaskUpdate } from './agentErrorAnalysis.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -122,6 +123,40 @@ describe('recovery output-hook dispatch (#3182)', () => {
getTaskOutputHook.mockResolvedValue(hook);
});
+ it('awaits the scheduled outcome ledger and finishes even when that write fails', async () => {
+ const { recordTaskTypeSuccess, recordTaskTypeFailure } = await import('./taskSchedule.js');
+ let rejectLedger;
+ recordTaskTypeSuccess.mockReturnValueOnce(new Promise((_resolve, reject) => { rejectLedger = reject; }));
+ const completion = finalizeAgent({
+ agentId: persistedAgent.id, task: TASK, success: true, exitCode: 0,
+ duration: 1000, outputBuffer: '',
+ });
+
+ await vi.waitFor(() => expect(recordTaskTypeSuccess).toHaveBeenCalledWith('example-output-hook', 'app-example'));
+ expect(updateTask).toHaveBeenCalledWith(TASK.id, { status: 'completed' }, 'internal');
+ expect(processAgentCompletion).not.toHaveBeenCalled();
+ rejectLedger(new Error('ledger write failed'));
+
+ await expect(completion).resolves.toMatchObject({ success: true });
+ expect(recordTaskTypeFailure).not.toHaveBeenCalled();
+ expect(processAgentCompletion).toHaveBeenCalledWith(persistedAgent.id, TASK, true, '');
+ expect(emitLog).toHaveBeenCalledWith('warn', expect.stringContaining('ledger write failed'), expect.any(Object));
+ });
+
+ it('records an unparseable scheduled output as a failure but excludes ad-hoc and terminated tasks', async () => {
+ const { recordTaskTypeSuccess, recordTaskTypeFailure } = await import('./taskSchedule.js');
+ hook.mockResolvedValueOnce({ recorded: false, reason: 'unparseable-response' });
+ await finalizeAgent({ agentId: persistedAgent.id, task: TASK, success: true, exitCode: 0, duration: 1000, outputBuffer: '' });
+ expect(recordTaskTypeFailure).toHaveBeenCalledWith('example-output-hook', 'app-example', { errorCategory: 'unparseable-response' });
+ expect(recordTaskTypeSuccess).not.toHaveBeenCalled();
+
+ recordTaskTypeFailure.mockClear();
+ await finalizeAgent({ agentId: 'ad-hoc', task: { id: 'ad-hoc-task', taskType: 'user', metadata: {} }, success: true, outputBuffer: '' });
+ await finalizeAgent({ agentId: 'terminated', task: TASK, success: false, terminatedByUser: true, outputBuffer: '' });
+ expect(recordTaskTypeFailure).not.toHaveBeenCalled();
+ expect(recordTaskTypeSuccess).not.toHaveBeenCalled();
+ });
+
it('persists scanner limits independently of successful assessment delivery and completion replay', async () => {
const scanner = { version: 1, status: 'time-limit', model: 'example-model', effort: 'low', auth: 'chatgpt',
maxCostUsd: null, estimatedCostUsd: null, timeLimitSeconds: 600, exitCode: 130,
diff --git a/server/services/agentFinalization.providerBench.test.js b/server/services/agentFinalization.providerBench.test.js
index 65a051b717..42ea820ded 100644
--- a/server/services/agentFinalization.providerBench.test.js
+++ b/server/services/agentFinalization.providerBench.test.js
@@ -91,6 +91,7 @@ import { finalizeAgent } from './agentFinalization.js';
import { detectImmediateFallbackSignal } from '../lib/aiToolkit/errorDetection.js';
import { analyzeAgentFailure } from './agentErrorAnalysis.js';
import { COOLDOWN_MS_BY_CATEGORY } from '../lib/providerCooldown.js';
+import { processAgentCompletion } from './agentCompletion.js';
const failedRun = (errorAnalysis, providerId = 'antigravity-tui') => finalizeAgent({
agentId: 'agent-1',
@@ -112,6 +113,21 @@ const failedRun = (errorAnalysis, providerId = 'antigravity-tui') => finalizeAge
beforeEach(() => vi.clearAllMocks());
describe('finalizeAgent provider sidelining', () => {
+ it('resolves a missing provider lazily and still completes when its cooldown cannot persist', async () => {
+ const analysis = detectImmediateFallbackSignal(
+ "We're finishing verifying your account eligibility. This usually takes a moment. Please try again shortly."
+ );
+ getActiveProviderMock.mockResolvedValueOnce({ id: 'fallback-provider' });
+ markProviderUnavailableMock.mockRejectedValueOnce(new Error('cooldown write failed'));
+
+ const outcome = await failedRun(analysis, null);
+
+ expect(getActiveProviderMock).toHaveBeenCalledOnce();
+ expect(markProviderUnavailableMock).toHaveBeenCalledWith('fallback-provider', expect.objectContaining({ reason: 'auth-error' }));
+ expect(outcome.success).toBe(false);
+ expect(processAgentCompletion).toHaveBeenCalledWith('agent-1', expect.any(Object), false, '');
+ });
+
it('benches the provider on the agy eligibility signal', async () => {
const analysis = detectImmediateFallbackSignal(
"We're finishing verifying your account eligibility. This usually takes a moment. Please try again shortly."