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: reconcile interrupted Stacker News executing actions (#11092)

Adam Eivyea257cebfd2c
diff --git a/server/services/stackerNews.js b/server/services/stackerNews.js
index cbba0807b5..1327b20e0d 100644
--- a/server/services/stackerNews.js
+++ b/server/services/stackerNews.js
@@ -36,6 +36,8 @@ const OLLAMA_ENDPOINT = 'http://127.0.0.1:11434/api/chat';
const UNTRUSTED_CONTENT_SOURCE = 'stacker-news';
const MAX_ANALYSIS_CHARS = 8_000;
const ACTION_MAX_AGE_MS = 24 * 60 * 60 * 1000;
+// An action stuck in `executing` this long was interrupted (crash/restart) between claim and finalize.
+const ACTION_EXECUTION_TIMEOUT_MS = 60 * 60 * 1000;
const DEFAULT_SYNC_ITEM_LIMIT = 30;
const MAX_SYNC_ITEM_LIMIT = 100;
const ITEM_HANDOFF_INTENTS = new Set(['inspect', 'zap', 'moderate']);
@@ -695,7 +697,17 @@ export async function createAction({ accountId, itemId = null, territoryId = nul
const existing = active.rows[0];
const approvalExpired = existing?.state === 'approved'
&& (!existing.approved_at || Date.now() - new Date(existing.approved_at).getTime() > ACTION_MAX_AGE_MS);
- if (existing && !approvalExpired) return existing;
+ const executionInterrupted = existing?.state === 'executing'
+ && (!existing.updated_at || Date.now() - new Date(existing.updated_at).getTime() > ACTION_EXECUTION_TIMEOUT_MS);
+ if (existing && !approvalExpired && !executionInterrupted) return existing;
+ if (executionInterrupted) {
+ // Release the active-idempotency unique index; the outcome is unknown, so a fresh human review is required.
+ await client.query(
+ "UPDATE stacker_news_actions SET state='failed',error='Execution interrupted before completion',updated_at=NOW() WHERE id=$1 AND state='executing'",
+ [existing.id],
+ );
+ await eventInsert(client, existing.id, 'executing', 'failed', 'Execution timed out or interrupted');
+ }
if (approvalExpired) {
await client.query(
"UPDATE stacker_news_actions SET state='rejected',error='Approval expired before execution',updated_at=NOW() WHERE id=$1 AND state='approved'",
diff --git a/server/services/stackerNewsActions.test.js b/server/services/stackerNewsActions.test.js
index 3756140961..a666ba2a25 100644
--- a/server/services/stackerNewsActions.test.js
+++ b/server/services/stackerNewsActions.test.js
@@ -74,4 +74,28 @@ describe('Stacker News reviewed actions', () => {
};
await expect(executeApprovedAction('action')).rejects.toThrow('External account or destination changed after review');
});
+
+ it('fails a stale executing action so it cannot wedge the idempotency key', async () => {
+ const stale = { id: 'zombie', state: 'executing', updated_at: new Date(Date.now() - 2 * 60 * 60 * 1000) };
+ transactionQuery.mockImplementationOnce(async () => ({ rows: [stale] }));
+ const action = await createAction({
+ accountId: account.id, itemId: item.id, kind: 'publish_comment', payload: { body: 'Retry after crash.' },
+ });
+ expect(action.state).toBe('pending_review');
+ expect(action.id).not.toBe('zombie');
+ const calls = transactionQuery.mock.calls.map(([sql, params]) => ({ sql, params }));
+ const fail = calls.find(({ sql }) => sql.includes("state='failed'"));
+ expect(fail.params).toEqual(['zombie']);
+ const event = calls.find(({ sql, params }) => sql.includes('stacker_news_action_events') && params?.includes('failed'));
+ expect(event.params).toContain('zombie');
+ });
+
+ it('keeps returning a recently started executing action', async () => {
+ const fresh = { id: 'running', account_id: account.id, state: 'executing', updated_at: new Date() };
+ transactionQuery.mockImplementationOnce(async () => ({ rows: [fresh] }));
+ const action = await createAction({
+ accountId: account.id, itemId: item.id, kind: 'publish_comment', payload: { body: 'In flight.' },
+ });
+ expect(action.id).toBe('running');
+ });
});