Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: a rollout hook that throws synchronously no longer leaves a live deadline timer

garrytan94c3c321f829
diff --git a/src/core/skillopt/validate-gate.ts b/src/core/skillopt/validate-gate.ts
index 0c404f0c1a..2c2b93027a 100644
--- a/src/core/skillopt/validate-gate.ts
+++ b/src/core/skillopt/validate-gate.ts
@@ -93,7 +93,7 @@ async function hookBatch(opts: ValidateGateOpts, hook: RolloutBatchHook, runsPer
reject(new Error(SKILLOPT_RUNTIME_EXCEEDED));
}, Math.max(0, deadline - Date.now()));
});
- const call = withoutBudgetTracker(() => hook({
+ const call = Promise.resolve().then(() => withoutBudgetTracker(() => hook({
phase: opts.phase ?? 'sel_gate',
skillText: opts.candidateSkillText,
skillSha256: createHash('sha256').update(opts.candidateSkillText).digest('hex'),
@@ -101,7 +101,7 @@ async function hookBatch(opts: ValidateGateOpts, hook: RolloutBatchHook, runsPer
runsPerTask,
epoch: opts.epoch ?? 0,
step: opts.step ?? 0,
- }, controller.signal));
+ }, controller.signal)));
let rows: ScoredRollout[][];
try {
rows = await (expired ? Promise.race([call, expired]) : call);
diff --git a/test/skillopt/benchmark-hooks.test.ts b/test/skillopt/benchmark-hooks.test.ts
index 4af8c57afe..23a5963940 100644
--- a/test/skillopt/benchmark-hooks.test.ts
+++ b/test/skillopt/benchmark-hooks.test.ts
@@ -79,6 +79,22 @@ describe('rollout hook (B1.3)', () => {
expect(signal?.aborted).toBe(true);
});
+ test('a hook that throws synchronously fails the gate and leaves no deadline timer behind', async () => {
+ const unhandled: unknown[] = [];
+ const onUnhandled = (e: unknown) => { unhandled.push(e); };
+ process.on('unhandledRejection', onUnhandled);
+ try {
+ const hook: RolloutBatchHook = () => { throw new Error('sync hook failure'); };
+ const err = await runValidationGate({ engine, candidateSkillText: 'S', selSet: tasks('s', 5), bestScore: 0, targetModel: 'm', rolloutHook: hook, deadlineMs: Date.now() + 30 })
+ .then(() => null, (e) => e);
+ expect(String(err?.message)).toContain('sync hook failure');
+ await new Promise((r) => setTimeout(r, 80));
+ expect(unhandled).toEqual([]);
+ } finally {
+ process.off('unhandledRejection', onUnhandled);
+ }
+ });
+
test('a batch answered after the runtime deadline is not a score', async () => {
const hook: RolloutBatchHook = async (req) => { await new Promise((r) => setTimeout(r, 30)); return req.tasks.map(() => [row(1), row(1), row(1)]); };
const deadlineMs = Date.now() + 10;