-
Notifications
You must be signed in to change notification settings - Fork 25
refactor: add a run-listener observer and adopt it in the cli #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
93dbd95
refactor: add a run-listener observer and adopt it in the cli
d62623a
fix: make the run-listener lifecycle fully symmetric
76025a4
fix: cover run setup with the listener lifecycle and isolate async li…
272d7fc
refactor: adopt the run-listener in the mcp runner and expose it in t…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // RunListener — an observer over a run's lifecycle (Observer / SPI). | ||
| // | ||
| // The engine emits lifecycle events; reporters, progress UIs, and telemetry | ||
| // adapters consume them by implementing this interface, so a new output format | ||
| // becomes a new listener with no engine edits. Every hook is optional — a | ||
| // listener implements only what it needs. | ||
|
|
||
| import type { UnifiedRunReport, ProgressEvent } from "./types.js"; | ||
| import { log } from "../lib/logger.js"; | ||
|
|
||
| /** The payload of a ProgressEvent variant, minus its `type` discriminant. */ | ||
| type Payload<T extends ProgressEvent["type"]> = Omit<Extract<ProgressEvent, { type: T }>, "type">; | ||
|
|
||
| export interface RunListener { | ||
| // Lifecycle: when onRunStart fires it is always paired with exactly one terminal | ||
| // hook — onRunFinish (the run produced a report, complete or partial) or | ||
| // onRunError (the run threw). A failure during run setup throws before onRunStart, | ||
| // so onRunError can also fire on its own. onRunStopped is a non-terminal notice: | ||
| // onRunFinish still follows it with the partial report. | ||
|
|
||
| /** Fired once at the start, before the target connects. Skipped if run setup fails first. */ | ||
| onRunStart?(info: { evaluatorCount: number }): void; | ||
| onEvaluatorStart?(info: Payload<"evaluator_start">): void; | ||
| onAttackStart?(info: Payload<"attack_start">): void; | ||
| onAttackDone?(info: Payload<"attack_done">): void; | ||
| onEvaluatorDone?(info: Payload<"evaluator_done">): void; | ||
| /** Non-terminal notice that a non-retryable error cut the run short; onRunFinish still follows. */ | ||
| onRunStopped?(info: Payload<"run_stopped">): void; | ||
| /** Terminal: the run threw (may fire without a preceding onRunStart on a setup failure). */ | ||
| onRunError?(info: { error: unknown }): void; | ||
| /** Terminal: fired once with the final report (complete, or partial after a stop). */ | ||
| onRunFinish?(report: UnifiedRunReport): void; | ||
| } | ||
|
|
||
| function warnListenerFailed(err: unknown): void { | ||
| log.warn( | ||
| `A RunListener hook failed and was skipped — fix the listener implementation: ${ | ||
| err instanceof Error ? err.message : String(err) | ||
| }` | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Invoke a hook on every listener with error isolation: a listener that throws — | ||
| * synchronously OR by rejecting an async hook — is logged and skipped, so a buggy | ||
| * reporter/telemetry adapter can never abort the run. Hooks are declared `void` | ||
| * (fire-and-forget); a returned promise is not awaited, but its rejection is caught. | ||
| * Centralizes the fan-out so every notification site is uniform. | ||
| */ | ||
| export function notifyListeners( | ||
| listeners: readonly RunListener[], | ||
| invoke: (listener: RunListener) => void | Promise<void> | ||
| ): void { | ||
| for (const listener of listeners) { | ||
| try { | ||
| const result = invoke(listener); | ||
| if (result && typeof (result as Promise<void>).then === "function") { | ||
| void (result as Promise<void>).then(undefined, warnListenerFailed); | ||
| } | ||
| } catch (err) { | ||
| warnListenerFailed(err); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Route one ProgressEvent to the matching RunListener hook. The engine keeps a | ||
| * single ProgressEvent stream as the source of per-attack events; this adapts it | ||
| * to the typed listener interface so callers don't switch on `event.type`. | ||
| */ | ||
| export function dispatchProgress(listener: RunListener, event: ProgressEvent): void { | ||
| switch (event.type) { | ||
| case "evaluator_start": | ||
| listener.onEvaluatorStart?.(event); | ||
| break; | ||
| case "attack_start": | ||
| listener.onAttackStart?.(event); | ||
| break; | ||
| case "attack_done": | ||
| listener.onAttackDone?.(event); | ||
| break; | ||
| case "evaluator_done": | ||
| listener.onEvaluatorDone?.(event); | ||
| break; | ||
| case "run_stopped": | ||
| listener.onRunStopped?.(event); | ||
| break; | ||
| default: { | ||
| // Compile-time exhaustiveness: a new ProgressEvent variant forces a case. | ||
| const _exhaustive: never = event; | ||
| void _exhaustive; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import type { Verdict } from "./judgeTypes.js"; | ||
|
|
||
| /** Terminal glyph for a verdict: ✓ pass, ✗ fail, ⚠ error. */ | ||
| export function verdictIcon(verdict: Verdict): string { | ||
| return verdict === "PASS" ? "✓" : verdict === "FAIL" ? "✗" : "⚠"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /** | ||
| * PR11 — RunListener SPI. | ||
| * | ||
| * Pins the ProgressEvent → RunListener adapter: each event type routes to exactly | ||
| * one hook with the event payload, and a listener that implements no hooks (or only | ||
| * some) is never called with a method it lacks — dispatchProgress must not throw. | ||
| */ | ||
| import { test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { dispatchProgress, notifyListeners, type RunListener } from "../src/execute/runListener.js"; | ||
| import type { ProgressEvent } from "../src/execute/types.js"; | ||
|
|
||
| function recorder() { | ||
| const calls: Array<{ hook: string; arg: unknown }> = []; | ||
| const listener: RunListener = { | ||
| onEvaluatorStart: (a) => calls.push({ hook: "onEvaluatorStart", arg: a }), | ||
| onAttackStart: (a) => calls.push({ hook: "onAttackStart", arg: a }), | ||
| onAttackDone: (a) => calls.push({ hook: "onAttackDone", arg: a }), | ||
| onEvaluatorDone: (a) => calls.push({ hook: "onEvaluatorDone", arg: a }), | ||
| onRunStopped: (a) => calls.push({ hook: "onRunStopped", arg: a }), | ||
| }; | ||
| return { listener, calls }; | ||
| } | ||
|
|
||
| test("each ProgressEvent routes to its matching hook with the payload", () => { | ||
| const { listener, calls } = recorder(); | ||
| const events: ProgressEvent[] = [ | ||
| { type: "evaluator_start", evaluatorId: "e1", evaluatorName: "Eval One" }, | ||
| { type: "attack_start", attackId: "a1", patternName: "pat" }, | ||
| { type: "attack_done", attackId: "a1", verdict: "FAIL" }, | ||
| { type: "evaluator_done", evaluatorId: "e1", passed: 1, failed: 2, errors: 0 }, | ||
| { type: "run_stopped", reason: "rate limited" }, | ||
| ]; | ||
| for (const e of events) dispatchProgress(listener, e); | ||
|
|
||
| assert.deepStrictEqual( | ||
| calls.map((c) => c.hook), | ||
| ["onEvaluatorStart", "onAttackStart", "onAttackDone", "onEvaluatorDone", "onRunStopped"] | ||
| ); | ||
| // Payload is forwarded verbatim (the discriminant `type` may ride along). | ||
| assert.deepStrictEqual(calls[0].arg, { | ||
| type: "evaluator_start", | ||
| evaluatorId: "e1", | ||
| evaluatorName: "Eval One", | ||
| }); | ||
| assert.deepStrictEqual(calls[2].arg, { type: "attack_done", attackId: "a1", verdict: "FAIL" }); | ||
| }); | ||
|
|
||
| test("a listener implementing no hooks is a safe no-op", () => { | ||
| const empty: RunListener = {}; | ||
| const events: ProgressEvent[] = [ | ||
| { type: "evaluator_start", evaluatorId: "e", evaluatorName: "n" }, | ||
| { type: "attack_done", attackId: "a", verdict: "PASS" }, | ||
| { type: "run_stopped", reason: "x" }, | ||
| ]; | ||
| for (const e of events) { | ||
| assert.doesNotThrow(() => dispatchProgress(empty, e)); | ||
| } | ||
| }); | ||
|
|
||
| test("notifyListeners isolates a throwing listener and still invokes the rest", () => { | ||
| const seen: string[] = []; | ||
| const bomb: RunListener = { | ||
| onRunStart: () => { | ||
| throw new Error("boom"); | ||
| }, | ||
| }; | ||
| const good: RunListener = { | ||
| onRunStart: (info) => seen.push(`ok:${info.evaluatorCount}`), | ||
| }; | ||
|
|
||
| // The thrower must not propagate, and the later listener must still run. | ||
| assert.doesNotThrow(() => | ||
| notifyListeners([bomb, good], (l) => l.onRunStart?.({ evaluatorCount: 3 })) | ||
| ); | ||
| assert.deepStrictEqual(seen, ["ok:3"]); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a stop, listeners get both
onRunStoppedand thenonRunFinish— this one isn't the final signal.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarified in d62623a. This is intentional — a reporter should still receive the partial report on a stop — but the docs were misleading.
onRunStoppedis now documented as a non-terminal notice (onRunFinishstill follows with the partial report);onRunFinish/onRunErrorare the two terminal hooks.