v0.3.0-rc.3: event-driven awareness + skill pre-emption
Adds a reactive layer on top of the polling reflex. The bot now
notices environmental shocks (forced moves, HP plunges, hostile
spawns) within ~100ms instead of waiting for the next DISPATCH tick,
and the in-flight skill is preempted so the next reflex cycle can
re-plan against the current world state.
This is the rc that wires the "rc.1 plumbing + rc.2 manifesto" into
a feedback loop:
- awareness fires preempt → dispatch aborts
- reflex tick re-evaluates → manifesto walks the ladder
- new dispatch picks the right skill for the new world state
Pieces:
- runtime/awareness/events.js (new) — bot.on listeners:
- move: single-tick Δposition ≥ 5 blocks → forced_move flag + preempt
- health: HP drop ≥ 2 → health_plunge flag + preempt
- entitySpawn: hostile mob within 12 blocks → hostile_added + preempt
- blockUpdate: nearby block change → env_changed flag (no preempt,
throttled 800ms; otherwise gather skills would self-preempt
every dig)
- runtime/skills/index.js — RUNNER_CODES.PREEMPTED + raceWithAbort()
wraps every execute() against ctx.abortSignal. Existing skills get
preemption for free; they don't have to check the signal manually.
- runtime/bot.js:
- dispatchAction creates a fresh AbortController per dispatch and
stores it on reflexCtx.currentAbort
- attachAwareness fires controller.abort() when something disrupts
the active skill; runSkill returns code: "preempted" and the
reflex moves on
- reflexCtx.lastPreempt records the most recent shock
Tests: 332 green (was 315 on rc.2, +17 new):
- runtime/awareness/events.test.js — 12 tests (each event type +
thresholds + throttling + passive-mob filter)
- runtime/skills/contract.test.js — 3 abortSignal tests
(mid-flight, pre-armed, clean signal)
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -166,3 +166,63 @@ test("result missing code defaults to runner DONE on success", async () => {
|
||||
teardown();
|
||||
}
|
||||
});
|
||||
|
||||
test("abortSignal: mid-execute abort surfaces code: preempted", async () => {
|
||||
const teardown = _registerForTest({
|
||||
id: "test.preempt-midflight",
|
||||
timeoutMs: 5000,
|
||||
preconditions: () => ({ ok: true }),
|
||||
execute: async () => {
|
||||
await new Promise((r) => setTimeout(r, 1500));
|
||||
return { ok: true };
|
||||
},
|
||||
});
|
||||
const controller = new AbortController();
|
||||
const runP = runSkill("test.preempt-midflight", { abortSignal: controller.signal });
|
||||
setTimeout(() => controller.abort(), 30);
|
||||
try {
|
||||
const res = await runP;
|
||||
assert.equal(res.ok, false);
|
||||
assert.equal(res.code, RUNNER_CODES.PREEMPTED);
|
||||
} finally {
|
||||
teardown();
|
||||
}
|
||||
});
|
||||
|
||||
test("abortSignal: pre-aborted signal short-circuits to preempted", async () => {
|
||||
const teardown = _registerForTest({
|
||||
id: "test.preempt-prearm",
|
||||
timeoutMs: 5000,
|
||||
preconditions: () => ({ ok: true }),
|
||||
execute: async () => {
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
return { ok: true };
|
||||
},
|
||||
});
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
try {
|
||||
const res = await runSkill("test.preempt-prearm", { abortSignal: controller.signal });
|
||||
assert.equal(res.ok, false);
|
||||
assert.equal(res.code, RUNNER_CODES.PREEMPTED);
|
||||
} finally {
|
||||
teardown();
|
||||
}
|
||||
});
|
||||
|
||||
test("abortSignal: not aborted → skill completes normally", async () => {
|
||||
const teardown = _registerForTest({
|
||||
id: "test.preempt-clear",
|
||||
timeoutMs: 5000,
|
||||
preconditions: () => ({ ok: true }),
|
||||
execute: async () => ({ ok: true, code: "done" }),
|
||||
});
|
||||
const controller = new AbortController();
|
||||
try {
|
||||
const res = await runSkill("test.preempt-clear", { abortSignal: controller.signal });
|
||||
assert.equal(res.ok, true);
|
||||
assert.equal(res.code, "done");
|
||||
} finally {
|
||||
teardown();
|
||||
}
|
||||
});
|
||||
|
||||
+46
-2
@@ -118,6 +118,7 @@ export const RUNNER_CODES = Object.freeze({
|
||||
TIMEOUT: "timeout",
|
||||
THREW: "threw",
|
||||
VALIDATION_FAILED: "validation_failed",
|
||||
PREEMPTED: "preempted",
|
||||
DONE: "done",
|
||||
});
|
||||
|
||||
@@ -139,6 +140,42 @@ function withTimeout(promise, ms, label) {
|
||||
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
|
||||
}
|
||||
|
||||
// v0.3.0-rc.3 — wrap execute() so that if ctx.abortSignal fires we
|
||||
// stop awaiting (and surface code: "preempted"). The skill itself
|
||||
// doesn't need to read the signal — the race below ensures runSkill
|
||||
// returns control to the reflex within one microtask of abort(). The
|
||||
// skill's own async work may continue in the background harmlessly,
|
||||
// because the next dispatch will overwrite any shared state.
|
||||
function raceWithAbort(promise, signal) {
|
||||
if (!signal) return promise;
|
||||
if (signal.aborted) {
|
||||
return Promise.reject(Object.assign(new Error("preempted"), { _preempted: true }));
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
let settled = false;
|
||||
const onAbort = () => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
reject(Object.assign(new Error("preempted"), { _preempted: true }));
|
||||
};
|
||||
signal.addEventListener("abort", onAbort, { once: true });
|
||||
promise.then(
|
||||
(v) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
signal.removeEventListener?.("abort", onAbort);
|
||||
resolve(v);
|
||||
},
|
||||
(e) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
signal.removeEventListener?.("abort", onAbort);
|
||||
reject(e);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// Drive one skill through its full lifecycle. The caller (typically reflex.js
|
||||
// or, eventually, a higher-level scheduler) decides when to invoke; runSkill
|
||||
// only owns the contract enforcement.
|
||||
@@ -172,12 +209,19 @@ export async function runSkill(id, ctx, args = {}) {
|
||||
const timeoutMs = skill.timeoutMs ?? 30_000;
|
||||
let raw;
|
||||
try {
|
||||
raw = await withTimeout(skill.execute(ctx, args), timeoutMs, `skill(${id})`);
|
||||
raw = await withTimeout(
|
||||
raceWithAbort(skill.execute(ctx, args), ctx?.abortSignal),
|
||||
timeoutMs,
|
||||
`skill(${id})`,
|
||||
);
|
||||
} catch (e) {
|
||||
const isTimeout = /timed out after/.test(e.message);
|
||||
const isPreempted = e?._preempted === true;
|
||||
const result = {
|
||||
ok: false,
|
||||
code: isTimeout ? RUNNER_CODES.TIMEOUT : RUNNER_CODES.THREW,
|
||||
code: isPreempted
|
||||
? RUNNER_CODES.PREEMPTED
|
||||
: isTimeout ? RUNNER_CODES.TIMEOUT : RUNNER_CODES.THREW,
|
||||
detail: e.message,
|
||||
worldDelta: null,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user