Cancelling an agent’s task should prevent queued work from starting. A check performed only when the plan was created cannot establish that property: permission may change before execution.
This exercise checks the task again immediately before a synthetic effect and reserves its call and byte budget synchronously. Run it with Node.js 22 or newer in a local environment you control. It makes no network requests and uses only fake payloads.
1. Guard the dispatch point
Save the program as dispatch-limits.mjs. Task state belongs to the trusted application. The payload is untrusted, so its size is measured with Buffer.byteLength instead of accepting a size estimate supplied by the model.
The budget counts attempted dispatches. It is deliberately not refunded when an effect fails, because a failed response does not establish that the action had no cost or consequence.
import assert from 'node:assert/strict';
const tasks = new Map();
const effects = [];
const now = Date.parse('2026-11-13T02:00:00Z');
const task = () => ({ active: true, expiresAt: now + 1000, callsLeft: 2, bytesLeft: 10 });
tasks.set('task-a', task());
tasks.set('task-b', task());
tasks.set('expired', { ...task(), expiresAt: now });
async function dispatch(taskId, payload, effect, at) {
assert(typeof payload === 'string' && payload.length > 0, 'invalid payload');
const size = Buffer.byteLength(payload, 'utf8');
assert(size <= 1000, 'payload too large');
assert(Number.isSafeInteger(at), 'invalid time');
const state = tasks.get(taskId);
assert(state?.active && at < state.expiresAt, 'task unavailable');
assert(state.callsLeft > 0 && state.bytesLeft >= size, 'budget exhausted');
// ponytail: one process, synchronous reservation; shared workers need atomic durable counters.
state.callsLeft -= 1;
state.bytesLeft -= size;
return effect(payload);
}
const record = async (payload) => { effects.push(payload); };
await dispatch('task-a', 'data', record, now);
assert.equal(effects.length, 1);
const queued = Promise.resolve().then(() => dispatch('task-a', 'data', record, now));
tasks.get('task-a').active = false;
await assert.rejects(queued, /task unavailable/);
assert.equal(effects.length, 1);
await assert.rejects(dispatch('expired', 'data', record, now), /task unavailable/);
await assert.rejects(dispatch('missing', 'data', record, now), /task unavailable/);
const results = await Promise.allSettled([
dispatch('task-b', 'data', record, now),
dispatch('task-b', 'data', record, now),
dispatch('task-b', 'data', record, now),
]);
assert.equal(results.filter((result) => result.status === 'fulfilled').length, 2);
assert.equal(effects.length, 3);
assert.equal(tasks.get('task-b').callsLeft, 0);
assert.equal(tasks.get('task-b').bytesLeft, 2);
tasks.set('tiny', { ...task(), bytesLeft: 1 });
await assert.rejects(dispatch('tiny', '\u00e9', record, now), /budget exhausted/);
assert.equal(tasks.get('tiny').callsLeft, 2);
await assert.rejects(dispatch('tiny', null, record, now), /invalid payload/);
assert.equal(effects.length, 3);
console.log('PASS: revoked and expired tasks stopped; concurrent calls and UTF-8 bytes stayed within budget.');
2. Run and inspect the assertions
node dispatch-limits.mjs
Expect one PASS: line. Task A completes one effect, then its queued action is rejected after cancellation. Task B allows two of three simultaneous attempts. The final case uses a character occupying two UTF-8 bytes to show why a character count is not a byte budget.
No rejected call reaches the effect recorder. That is a stronger observation than checking whether an agent prints a cancellation message.
3. Make cancellation fail on purpose
In a temporary copy, replace state?.active && at < state.expiresAt with state && at < state.expiresAt. Run it again. The queued-action assertion must fail because cancellation is no longer checked.
Restore the active-state condition afterward. You can similarly remove the budget assertion to verify that the concurrency case detects excess dispatches.
4. Know what cancellation cannot undo
This guard stops new dispatches. It cannot retract an effect already in progress or remove data already delivered. Real tools need their own supported cancellation behavior, reconciliation, and incident controls.
The counters are local and disappear on restart. Multiple workers require a shared atomic reservation and a policy for expiry, retention, and uncertain outcomes. The trusted dispatcher must also supply the task identity and clock; neither should come from an agent’s proposed arguments.
Use this test as one part of incident containment, alongside credential revocation and evidence collection. Its claim is limited and testable: after a task is cancelled, the next dispatch through this guard does not start a new effect.
