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: guard runtime recovery against the fresh verified task snapshot

[._.]/ Adam Eivy14d8db9dc664
diff --git a/server/services/agentFinalization.js b/server/services/agentFinalization.js
index 22d9785da7..8c0b758979 100644
--- a/server/services/agentFinalization.js
+++ b/server/services/agentFinalization.js
@@ -1306,9 +1306,11 @@ export async function retireDeadAgent({ agent, task, success, exitCode, duration
let recoveryOwnershipLost = false;
if (recoveryUpdate) {
const update = await updateTask(task.id, recoveryUpdate, task.taskType || 'user', {
- ...runtimeRecoveryFence(recoveryUpdate)[0],
expectedStatus: task.status || null, expectedTask: { description: task.description, priority: task.priority, metadata: task.metadata },
recoveryOwner: { id: agent.id, startedAt: agent.startedAt },
+ // Runtime resolution reloads the dispatch; guard its fresh snapshot rather
+ // than the older task initially supplied to this recovered run.
+ ...runtimeRecoveryFence(recoveryUpdate)[0],
});
if (update?.error) throw new Error(`Could not persist recovered task outcome: ${update.error}`);
if (update?.statusChanged && recoveryUpdate.metadata?.blockedCategory === 'runtime-verification') {
@@ -2454,7 +2456,7 @@ async function resolveDeliveryRecoveryUpdate(task, outcome, agentId) {
function runtimeRecoveryFence(update) {
if (update.metadata?.blockedCategory !== 'runtime-verification') return [];
const p = update.metadata.runtimeVerification;
- return [{ expectedStatus: p.dispatchStatus, expectedMetadata: {
+ return [{ ...update.runtimeRecoveryContext, expectedStatus: p.dispatchStatus, expectedMetadata: {
...(p.dispatchStatus === 'blocked' ? { runtimeVerificationId: update.metadata.runtimeVerificationId } : {}),
...Object.fromEntries(['claimedBy', 'claimedAt', 'lastSpawnedAt', 'totalSpawnCount'].map(key => [key, p.dispatch?.[key]])),
} }];
diff --git a/server/services/agentFinalization.outputHooks.test.js b/server/services/agentFinalization.outputHooks.test.js
index c0a2ab5903..2ef6f96008 100644
--- a/server/services/agentFinalization.outputHooks.test.js
+++ b/server/services/agentFinalization.outputHooks.test.js
@@ -523,7 +523,7 @@ describe('recovery output-hook dispatch (#3182)', () => {
blockedCategory: 'runtime-verification', failureCount: 1, totalSpawnCount: 3,
runtimeVerification: expect.objectContaining({ taskId: task.id, agentId: persistedAgent.id, integratedCommit: 'c'.repeat(40) }),
}) }), 'internal', expect.objectContaining({ expectedStatus: 'in_progress' }));
- if (mode === 'recovered') expect(updateTask).toHaveBeenCalledWith(task.id, expect.objectContaining({ status: 'blocked' }), 'internal', {
+ expect(updateTask).toHaveBeenCalledWith(task.id, expect.objectContaining({ status: 'blocked' }), 'internal', {
expectedStatus: 'in_progress',
expectedTask: { description: task.description, priority: task.priority, metadata: task.metadata },
recoveryOwner: { id: persistedAgent.id, startedAt: persistedAgent.startedAt },
diff --git a/server/services/runtimeVerification.js b/server/services/runtimeVerification.js
index b16539576f..8694593061 100644
--- a/server/services/runtimeVerification.js
+++ b/server/services/runtimeVerification.js
@@ -66,6 +66,14 @@ export async function resolveRuntimeVerificationUpdate(task, outcome, agentId) {
if (!live || owner?.taskId !== task.id || (!replay && (live.status !== 'in_progress'
|| !matchesCompletionTaskDispatch(owner, live)))) throw new Error('Runtime verification dispatch ownership is unverified');
task = live;
+ // Internal publication context, separate from metadata: the caller forwards it
+ // to updateTask's options; the task-store projection never persists this field.
+ // Snapshot before forge awaits so later edits cannot redefine observed authority.
+ const runtimeRecoveryContext = {
+ expectedStatus: task.status,
+ expectedTask: structuredClone({ description: task.description, priority: task.priority, metadata: task.metadata }),
+ recoveryOwner: { id: agentId, startedAt: owner.startedAt },
+ };
const prior = task.metadata?.runtimeVerification;
// Replaying finalization retains the original dispatch identity and evidence.
const same = prior?.agentId === agentId && task.metadata?.runtimeVerificationId === `${task.id}:${agentId}`;
@@ -83,7 +91,7 @@ export async function resolveRuntimeVerificationUpdate(task, outcome, agentId) {
reason = await prerequisiteReason(prerequisite);
} else reason = 'Runtime prerequisite retained: merged-fix integration evidence is unreadable or invalid';
}
- return { status: 'blocked', metadata: { ...task.metadata,
+ return { status: 'blocked', runtimeRecoveryContext, metadata: { ...task.metadata,
runtimeVerificationId: `${task.id}:${agentId}`, runtimeVerification: { ...prerequisite, dispatchStatus: task.status,
dispatch: Object.fromEntries(['claimedBy', 'claimedAt', 'lastSpawnedAt', 'totalSpawnCount']
.filter(key => task.metadata?.[key] !== undefined).map(key => [key, task.metadata[key]])) },
diff --git a/server/services/runtimeVerification.test.js b/server/services/runtimeVerification.test.js
index ea3293d2fc..c99945244b 100644
--- a/server/services/runtimeVerification.test.js
+++ b/server/services/runtimeVerification.test.js
@@ -6,7 +6,7 @@ const facts = vi.hoisted(() => ({ boot: 'b'.repeat(40), integrated: 'c'.repeat(4
owner: { id: 'agent-example', taskId: 'sys-example', status: 'completed', metadata: {} },
trusted: true, running: false, forge: true, ancestor: false }));
vi.mock('./cosState.js', async () => ({
- loadState: vi.fn(async () => ({ agents: {} })),
+ loadState: vi.fn(async () => ({ agents: facts.running ? { sibling: { id: 'sibling', taskId: 'sys-example', status: 'running' } } : {} })),
withStateLock: (await import('../lib/fileWriteQueue.js')).createFileWriteQueue(),
readAgentsStateForSafetyCheck: vi.fn(async () => ({ trusted: facts.trusted,
agents: facts.running ? { sibling: { taskId: 'sys-example', status: 'running' } } : {} })),
@@ -33,7 +33,7 @@ vi.mock('./agentWorktreeCleanup.js', () => ({ releaseRetryHold: vi.fn(async ({ t
}) }));
import { resolveRuntimeVerificationUpdate, sweepRuntimeVerifications } from './runtimeVerification.js';
-import { addTask, getTaskById, updateTask, reviveBlockedTask, __resetTaskCache, sweepResolvedFailureTasks } from './cosTaskStore.js';
+import { addTask, getTaskById, updateTask, deleteTask, reviveBlockedTask, __resetTaskCache, sweepResolvedFailureTasks } from './cosTaskStore.js';
import { __resetCosTaskRepository } from './cosTaskRepository.js';
import { cosEvents } from './cosEvents.js';
import { normalizeTaskOutcome } from '../lib/agentOutcome.js';
@@ -52,7 +52,8 @@ async function runningTask() {
}
async function park({ ready = true } = {}) {
const task = await runningTask();
- await updateTask(task.id, await resolveRuntimeVerificationUpdate({ ...task, metadata: { totalSpawnCount: 0 } }, outcome(), 'agent-example'), 'internal');
+ const update = await resolveRuntimeVerificationUpdate({ ...task, metadata: { totalSpawnCount: 0 } }, outcome(), 'agent-example');
+ await updateTask(task.id, update, 'internal', update.runtimeRecoveryContext);
if (ready) await updateTask(task.id, { metadata: { runtimeVerificationReadyAgentId: 'agent-example' } }, 'internal');
return getTaskById(task.id);
}
@@ -67,6 +68,9 @@ describe('persisted runtime continuation', () => {
it('parks stale code without budget changes, expiry, generic revival or affected-task completion', async () => {
const task = await park();
expect(task).toMatchObject({ status: 'blocked', metadata: { blockedCategory: 'runtime-verification', failureCount: '2', totalSpawnCount: '4' } });
+ expect(task).not.toHaveProperty('runtimeRecoveryContext');
+ expect(task.metadata).not.toHaveProperty('runtimeRecoveryContext');
+ expect(JSON.stringify(cosEvents.emit.mock.calls)).not.toContain('runtimeRecoveryContext');
expect(task.metadata.blockedReason).toContain('Automatic updates are disabled');
expect(await reviveBlockedTask(task.id)).toMatchObject({ error: expect.stringContaining('Runtime verification') });
await sweepResolvedFailureTasks({ now: Date.now() + 60 * 86400000 });
@@ -126,6 +130,39 @@ describe('persisted runtime continuation', () => {
expect((await getTaskById(task.id)).status).toBe('in_progress');
});
+ it.each(['description', 'priority', 'manual-blocker', 'new-owner'])('preserves a %s change made during runtime prerequisite inspection', async kind => {
+ const task = await runningTask();
+ const observed = await getTaskById(task.id);
+ const originalForge = execGh.getMockImplementation();
+ let entered, release;
+ const inspecting = new Promise(resolve => { entered = resolve; });
+ const inspection = new Promise(resolve => { release = resolve; });
+ execGh.mockImplementationOnce(async (...args) => { entered(); await inspection; return originalForge(...args); });
+ const pending = resolveRuntimeVerificationUpdate(task, outcome(), 'agent-example');
+ await inspecting;
+ if (kind === 'description') await updateTask(task.id, { description: 'Operator revised the objective' }, 'internal');
+ if (kind === 'priority') await updateTask(task.id, { priority: 'HIGH' }, 'internal');
+ if (kind === 'manual-blocker') await updateTask(task.id, { metadata: { blocker: 'Operator hold' } }, 'internal');
+ if (kind === 'new-owner') facts.running = true;
+ const retained = await getTaskById(task.id);
+ release();
+ const update = await pending;
+ expect(update.runtimeRecoveryContext.expectedTask).toEqual({ description: observed.description, priority: observed.priority, metadata: observed.metadata });
+ expect(await updateTask(task.id, update, 'internal', update.runtimeRecoveryContext)).toMatchObject({ statusChanged: true });
+ __resetTaskCache();
+ expect(await getTaskById(task.id)).toEqual(retained);
+ expect((await getTaskById(task.id)).metadata).not.toHaveProperty('runtimeVerification');
+ });
+
+ it.each(['task', 'owner'])('refuses runtime prerequisite publication when its %s is missing', async kind => {
+ const task = await runningTask();
+ if (kind === 'task') await deleteTask(task.id, 'internal');
+ else facts.owner = null;
+ await expect(resolveRuntimeVerificationUpdate(task, outcome(), 'agent-example')).rejects.toThrow('dispatch ownership');
+ if (kind === 'task') expect(await getTaskById(task.id)).toBeNull();
+ else expect((await getTaskById(task.id)).metadata).not.toHaveProperty('runtimeVerification');
+ });
+
it('retains unreadable integration as a prerequisite without spending another run, then verifies it afresh', async () => {
facts.forge = false; await park(); facts.ancestor = true;
expect((await getTaskById('sys-example')).metadata.blockedReason).toContain('unreadable or invalid');