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: keep timed-out output hooks recoverable (#3182)

[._.]/ Adam Eivyc04cbf913878
diff --git a/server/services/agentFinalization.js b/server/services/agentFinalization.js
index 7821608160..0d1d8ac9ed 100644
--- a/server/services/agentFinalization.js
+++ b/server/services/agentFinalization.js
@@ -235,38 +235,61 @@ export function dispatchTaskOutputHookOnce({
const existing = outputHookDispatches.get(agentId);
if (existing) return existing;
+ const persistDispatchMarker = async (result) => {
+ if (!result.ran) return;
+ // Best-effort durability: completion must continue if the marker write
+ // fails, while the in-flight promise still protects concurrent callers
+ // this cycle.
+ await updateAgent(agentId, {
+ metadata: { outputHookDispatchedAt: new Date().toISOString() }
+ }).catch(err => {
+ emitLog('warn', `⚠️ Failed to persist output-hook dispatch marker for ${agentId}: ${err.message}`, { agentId });
+ });
+ };
+
const dispatch = (async () => {
const agent = await getAgent(agentId).catch(() => null);
if (agent?.metadata?.outputHookDispatchedAt) {
return { ran: false, alreadyDispatched: true };
}
- const result = await withOutputHookTimeout(
- dispatchTaskOutputHook({ agentId, task, success, workspacePath, readPayload, recovery }),
- { agentId }
- ).catch(err => {
+ const hookDispatch = dispatchTaskOutputHook({
+ agentId,
+ task,
+ success,
+ workspacePath,
+ readPayload,
+ recovery,
+ }).catch(err => {
emitLog('error', `❌ processTaskOutput hook threw for ${agentId} (${task?.taskType}): ${err.message}`, { agentId, error: err.message });
return { ran: true, threw: true };
});
+ const result = await withOutputHookTimeout(hookDispatch, { agentId });
- // A missing task/type/hook is not a dispatch and must remain retryable if a
- // later completion path has fuller context. A timeout does count: the hook
- // keeps running after Promise.race releases the completion path.
- if (result.ran || result.timedOut) {
- // Best-effort durability: completion must continue if the marker write
- // fails, while the in-flight promise still protects concurrent callers
- // this cycle.
- await updateAgent(agentId, {
- metadata: { outputHookDispatchedAt: new Date().toISOString() }
- }).catch(err => {
- emitLog('warn', `⚠️ Failed to persist output-hook dispatch marker for ${agentId}: ${err.message}`, { agentId });
- });
+ if (result.timedOut) {
+ // The hook is still running. Keep this dispatch in the in-process map so
+ // another completion path cannot start a duplicate, and persist the
+ // durable marker only after the original hook actually settles. If the
+ // process exits first, restart recovery remains free to retry it.
+ hookDispatch
+ .then(persistDispatchMarker)
+ .finally(() => {
+ if (outputHookDispatches.get(agentId) === dispatch) {
+ outputHookDispatches.delete(agentId);
+ }
+ });
+ } else {
+ await persistDispatchMarker(result);
}
return result;
})();
outputHookDispatches.set(agentId, dispatch);
- dispatch.finally(() => {
+ dispatch.then(result => {
+ if (!result.timedOut && outputHookDispatches.get(agentId) === dispatch) {
+ outputHookDispatches.delete(agentId);
+ }
+ }).catch(() => {
if (outputHookDispatches.get(agentId) === dispatch) {
outputHookDispatches.delete(agentId);
}
diff --git a/server/services/agentFinalization.outputHooks.test.js b/server/services/agentFinalization.outputHooks.test.js
index 2c7f91b3fe..2c69b1829e 100644
--- a/server/services/agentFinalization.outputHooks.test.js
+++ b/server/services/agentFinalization.outputHooks.test.js
@@ -115,6 +115,41 @@ describe('recovery output-hook dispatch (#3182)', () => {
expect(hook).toHaveBeenCalledOnce();
});
+ it('keeps a timed-out hook recoverable until the original dispatch settles', async () => {
+ vi.useFakeTimers();
+ let finishHook;
+ hook.mockReturnValueOnce(new Promise(resolve => { finishHook = resolve; }));
+
+ const original = dispatchTaskOutputHookOnce({
+ agentId: persistedAgent.id,
+ task: TASK,
+ success: true,
+ });
+ await vi.advanceTimersByTimeAsync(5 * 60_000);
+
+ await expect(original).resolves.toEqual({ ran: false, timedOut: true });
+ expect(updateAgent).not.toHaveBeenCalled();
+ expect(dispatchRecoveredTaskOutputHook({
+ agentId: persistedAgent.id,
+ task: TASK,
+ success: false,
+ })).toBe(original);
+
+ finishHook({ recorded: true });
+ await vi.waitFor(() => expect(updateAgent).toHaveBeenCalledWith(
+ persistedAgent.id,
+ { metadata: { outputHookDispatchedAt: expect.any(String) } },
+ ));
+ vi.useRealTimers();
+
+ await expect(dispatchRecoveredTaskOutputHook({
+ agentId: persistedAgent.id,
+ task: TASK,
+ success: false,
+ })).resolves.toEqual({ ran: false, alreadyDispatched: true });
+ expect(hook).toHaveBeenCalledOnce();
+ });
+
it('does not dispatch after finalizeAgent persisted the explicit hook marker', async () => {
persistedAgent.metadata.outputHookDispatchedAt = new Date().toISOString();