best

best executes a work command multiple times, scores each output through a scoring command, and returns the highest-scoring result.

Syntax

@result best retriesNo scoreCommand workCommand arg1 arg2 ...

Example

@counter jsdef
if(global.counter === undefined){ global.counter = 0; }
global.counter++;
return global.counter;
end

@generate macro hello ~world
    @orderNumber counter
    @res := $hello $world $orderNumber
    return $res
end

@verify jsdef value
    if (value === "Hello World 5") { return 10; }
    return 1;
end

@result best 7 verify generate Hello

Validated in tests/pipelines/macros/basic/bestCommandTest.mjs. The command retries generation up to 7 times and picks Hello World 5 because its score reaches 10.

Step-by-Step Interaction

The command starts by reading the retry count, the scoring command name, the work command name, and any additional arguments that should be forwarded to the work command. For every iteration, the runtime executes the work command, then immediately executes the scoring command using the work result as scoring input. The runtime compares the numeric score with the current best score and keeps the highest-scoring result across the loop. When score reaches the terminal threshold value 10, the loop exits early and returns the current best result.

Runtime Behavior Notes

The first argument must be a valid numeric retry count, otherwise the command records an invalid syntax error and produces undefined output. The scorer output is parsed as floating-point value so ranking works for both integer and decimal scores. The implementation intentionally catches iteration-level execution errors, logs them for diagnostics, and continues with remaining attempts instead of aborting the full command. This behavior gives best a robust search profile for non-deterministic generation tasks.