Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Stop two tests from failing on slow CI runners (#9397)
Garry Tan5354aed6890e
diff --git a/changelog.d/capy-fix-proactive-brief-and-hook-lane-flakes.md b/changelog.d/capy-fix-proactive-brief-and-hook-lane-flakes.mdnew file mode 100644index 0000000000..74e56c8a83--- /dev/null+++ b/changelog.d/capy-fix-proactive-brief-and-hook-lane-flakes.md@@ -0,0 +1,6 @@+**Two tests that failed CI runs on unrelated changes now pass on a busy runner.** Neither changed what it checks.++### Tests++- `test/proactive-brief.test.ts`: the two tests that check a delivered brief writes its `context_volunteer_events` rows went through `turnContextWithBrief`, which gives the brief only 300 ms of wall clock from the start of the request. On a slow runner the base assembly used up that time, the brief came back empty, nothing was delivered or logged, and the row check saw `[]`. `turnContextWithBrief` now takes an optional clock (it still defaults to the 300 ms cutoff), and those two tests pass the same wide test clock the file's other brief tests already use. A probe that slows every query by 60 ms fails the old test and passes the new one.+- `test/skill-resolver-hook-lane.test.ts`: the 5 MB Hermes history test compared one timed run of a small turn with one timed run of the 5 MB turn against the 50 ms lane deadline. Each run varies by 20 to 30 ms on an idle machine and more on a loaded one, so one descheduled turn could fail it (it failed 1 run in 5 under load locally). It now times three runs of each and compares the fastest of each, with the same 50 ms bar.diff --git a/src/mcp/resolve-ipc-binding.ts b/src/mcp/resolve-ipc-binding.tsindex d2755f566d..bc1f288b10 100644--- a/src/mcp/resolve-ipc-binding.ts+++ b/src/mcp/resolve-ipc-binding.ts@@ -34,7 +34,9 @@ import { resolveEntitiesToPointers, logDeliveredReflexPointers } from '../core/cimport { lexicalArmsEnabled } from '../core/context/reflex.ts';import { VOLUNTEER_MAX_PAGES_CAP } from '../core/context/volunteer.ts';import { assembleTurnContext, TURN_CONTEXT_DEFAULT_MAX_BYTES, type TurnContextResult } from '../core/context/turn-context.ts';-import { attachBrief, briefEnabled, BRIEF_MAX_BYTES, recordBriefDelivered, turnBrief, TURN_BRIEF_CUTOFF_MS, withBriefDeadline } from '../core/context/proactive-brief.ts';+import { attachBrief, briefEnabled, BRIEF_MAX_BYTES, recordBriefDelivered, turnBrief, TURN_BRIEF_CUTOFF_MS, withBriefDeadline, type BriefClock } from '../core/context/proactive-brief.ts';++const turnBriefClock = (now = Date.now()): BriefClock => ({ now, stopAt: now + TURN_BRIEF_CUTOFF_MS });import { proactiveEligibility } from '../core/eligibility/registry.ts';import { readHotMemorySessionOnly } from '../core/facts/meta-hook.ts';import { makeContextPackIpcHandler } from './context-pack-handler.ts';@@ -69,12 +71,11 @@ function defaultRebindDelayMs(): number {* Wave 5 D5 proactive brief (default off): the legacy block is assembled* first and stands on its own; when `context.proactive_brief` is on, the* brief runs in what is left of a request-start cutoff (its queries stop once- * it passes), inside a byte reservation taken from the legacy block, and+ * it passes; `clock` defaults to that cutoff from now), inside a byte reservation taken from the legacy block, and* leads the text. Off, this is the legacy assembly exactly. Always assembles* against the serve's OWN registered source [CX2-10].*/-export async function turnContextWithBrief(engine: BrainEngine, sourceId: string, req: TurnContextRequest): Promise<TurnContextResult> {- const clock = { now: Date.now(), stopAt: Date.now() + TURN_BRIEF_CUTOFF_MS };+export async function turnContextWithBrief(engine: BrainEngine, sourceId: string, req: TurnContextRequest, clock: BriefClock = turnBriefClock()): Promise<TurnContextResult> {const briefOn = await briefEnabled(engine);const base = await assembleTurnContext(engine, {sourceId,diff --git a/test/proactive-brief.test.ts b/test/proactive-brief.test.tsindex e5e0a99d81..28df2c2cec 100644--- a/test/proactive-brief.test.ts+++ b/test/proactive-brief.test.ts@@ -4,7 +4,7 @@* telemetry rows, and the default-off guarantee (with the key unset every* surface answers exactly as before). Synthetic data only.*/-import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';+import { describe, test, expect, beforeAll, afterAll, beforeEach, spyOn } from 'bun:test';import { PGLiteEngine } from '../src/core/pglite-engine.ts';import { resetPgliteState } from './helpers/reset-pglite.ts';import { normalizeAlias } from '../src/core/search/alias-normalize.ts';@@ -28,6 +28,7 @@ import {assembleBrief,sessionStartBrief,turnBrief,+ TURN_BRIEF_CUTOFF_MS,withBriefDeadline,type Brief,type BriefClock,@@ -346,7 +347,7 @@ describe('page edits (brains that record a change by editing pages)', () => {await rewrite('people/alice-example', 'Alice Example', BEFORE);await rewrite('people/alice-example', 'Alice Example', AFTER);const [{ id }] = await engine.executeRaw<{ id: number }>(`SELECT max(id) AS id FROM page_versions`);- const base = await turnContextWithBrief(engine, 'default', { kind: 'turn_context', protocol: 2, secret: 'x', sessionId: 'se', window: [{ role: 'user', text: 'Draft a reply to Alice Example' }] });+ const base = await turnContextWithBrief(engine, 'default', { kind: 'turn_context', protocol: 2, secret: 'x', sessionId: 'se', window: [{ role: 'user', text: 'Draft a reply to Alice Example' }] }, clock());briefDelivered(engine, 'default', base, 'se');await awaitPendingVolunteerEventWrites();const rows = await engine.executeRaw<{ match_arm: string; rationale: string }>(`SELECT match_arm, rationale FROM context_volunteer_events WHERE channel = 'brief'`);@@ -542,7 +543,7 @@ describe('repeat suppression', () => {describe('telemetry', () => {test('a delivered brief logs one row per item under channel brief', async () => {const id = await fact('Moved to Acme Example as CFO.', 'people/alice-example');- const base = await turnContextWithBrief(engine, 'default', { kind: 'turn_context', protocol: 2, secret: 'x', sessionId: 'st', window: [{ role: 'user', text: 'Draft a reply to Alice Example' }] });+ const base = await turnContextWithBrief(engine, 'default', { kind: 'turn_context', protocol: 2, secret: 'x', sessionId: 'st', window: [{ role: 'user', text: 'Draft a reply to Alice Example' }] }, clock());expect(base.brief?.trigger).toBe('lexicon');expect(base.text.startsWith(`${TURN_CONTEXT_ENVELOPE}\n\n${BRIEF_HEADER}`)).toBe(true);briefDelivered(engine, 'default', base, 'st');@@ -551,6 +552,21 @@ describe('telemetry', () => {`SELECT channel, match_arm, rationale, slug, session_id FROM context_volunteer_events WHERE channel = 'brief'`);expect(rows).toEqual([{ channel: 'brief', match_arm: 'brief-lexicon', rationale: `change:F${id}`, slug: 'people/alice-example', session_id: 'st' }]);});++ test('without a clock, turn_context stops brief work 300 ms into the request', async () => {+ await fact('Moved to Acme Example as CFO.', 'people/alice-example');+ const turn = (c?: BriefClock) => turnContextWithBrief(engine, 'default', { kind: 'turn_context', protocol: 2, secret: 'x', sessionId: 'cut', window: [{ role: 'user', text: 'Draft a reply to Alice Example' }] }, c);+ const start = Date.now();+ let calls = 0;+ const now = spyOn(Date, 'now').mockImplementation(() => (calls++ === 0 ? start : start + TURN_BRIEF_CUTOFF_MS + 1));+ try {+ expect(TURN_BRIEF_CUTOFF_MS).toBe(300);+ expect((await turn()).brief).toBeUndefined();+ expect((await turn(clock(start))).brief?.trigger).toBe('lexicon');+ } finally {+ now.mockRestore();+ }+ });});describe('pull params', () => {diff --git a/test/skill-resolver-hook-lane.test.ts b/test/skill-resolver-hook-lane.test.tsindex 6148682882..d172661f65 100644--- a/test/skill-resolver-hook-lane.test.ts+++ b/test/skill-resolver-hook-lane.test.ts@@ -632,17 +632,24 @@ describe('skill-resolver lane: hermes', () => {test('a 5 MB conversation_history before and after user_message is read, never refused, and still delivers within a turn budget', async () => {const b = await hermesBox();- const small = await hook(b, hermesCmd(b), { session_id: 'h2a', extra: { user_message: 'please deploy the widget to staging' } }, WIDE);- expect(JSON.parse(small.stdout).context).toContain('id: "deploy-widget"');- const turn = { session_id: 'h2', extra: { conversation_history: history(2.5e6), user_message: 'please deploy the widget to staging', later_history: history(2.5e6) } };- expect(JSON.stringify(turn).length).toBeGreaterThan(5e6);- const r = await hook(b, hermesCmd(b), turn, WIDE);- expect(r.exitCode).toBe(0);- expect(JSON.parse(r.stdout).context).toContain('id: "deploy-widget"');- const [base, big] = laneLog(b).slice(-2);- expect(big).toMatchObject({ outcome: 'delivered' });+ const before = history(2.5e6), after = history(2.5e6);+ const smallMs: number[] = [], bigMs: number[] = [];+ for (let n = 0; n < 3; n++) {+ const small = await hook(b, hermesCmd(b), { session_id: `h2a-${n}`, extra: { user_message: 'please deploy the widget to staging' } }, WIDE);+ expect(JSON.parse(small.stdout).context).toContain('id: "deploy-widget"');+ smallMs.push(laneLog(b).at(-1).ms);+ const turn = { session_id: `h2-${n}`, extra: { conversation_history: before, user_message: 'please deploy the widget to staging', later_history: after } };+ expect(JSON.stringify(turn).length).toBeGreaterThan(5e6);+ const r = await hook(b, hermesCmd(b), turn, WIDE);+ expect(r.exitCode).toBe(0);+ expect(JSON.parse(r.stdout).context).toContain('id: "deploy-widget"');+ const big = laneLog(b).at(-1);+ expect(big).toMatchObject({ outcome: 'delivered' });+ bigMs.push(big.ms);+ }// Reading 5 MB adds less than one lane deadline to a small turn; a buffering or quadratic reader would not.- expect(big.ms - base.ms).toBeLessThan(LANE_DEADLINE_MS);+ // Each side's fastest of three runs, so one turn a busy machine descheduled cannot decide it.+ expect(Math.min(...bigMs) - Math.min(...smallMs)).toBeLessThan(LANE_DEADLINE_MS);});test('malformed Hermes JSON fails open: exit 0, no stdout, skill_resolve_no_input', async () => {