hr_step_command() builds a step that runs one program you name, such as
Rscript on a test file, and reads its result as "passed" or
"failed". Put it in a chain after the step that writes what it checks,
so a benchmark can count how often a draft passes.
Usage
hr_step_command(
name,
argv,
parse = c("exit", "testthat"),
timeout = 60,
held = FALSE,
wd = NULL
)Arguments
- name
The step's name, one string.
- argv
The command: a character vector whose first element is the program and the rest its arguments, or a function of
(input, ctx)that returns one, such as a vector that names a file the step before it wrote.- parse
How the result is read:
"exit"or"testthat".- timeout
Seconds the program may run before it is stopped, from 1 to 86400.
- held
Whether the result is kept from the steps after this one and left for the scorer.
- wd
The folder the program runs in, or
NULLfor the current one. The working directory is set back afterwards.
Value
A step (see hr_step()) of class
c("hr_step_command", "hr_step", "function") with the settings
parse, timeout and held, which hr_set() and hr_sweep() change
by path, as "check.timeout". The result it records is a list of class
hr_command_result: name, argv, status, exit (the exit code, or
NA), seconds, degradation (NA unless timed out), counts (for
"testthat": fail, warn, skip and pass, else NULL), and
stdout and stderr, each cut to its last 64 KB.
Details
The command runs through base system2() with the user's own privileges.
Each element of argv is one argument: they are quoted for the shell,
never split on spaces, so a ; or a $ in an argument reaches the
program as it is written. There is no default command, and a command is
never taken from a model's reply or a skill's text: write argv yourself.
Nothing isolates the program: it can read and write what you can. It
inherits the session's environment variables except the ones that look
like credentials (see "Credentials" below); R options never reach it. A
program stopped at its time limit is ended,
but processes it started in the background (or that ignore the signal)
can keep running, on any platform; on Windows, any process it started
can.
The result is read by parse:
"exit""passed"when the program exits with 0, else"failed"."testthat"The last
[ FAIL n | WARN n | SKIP n | PASS n ]line the program printed:"failed"whenFAILis above 0,"passed"when it is 0 and the program exits with 0. With no such line, or a summary of no failures and a non-zero exit, the status is"error". The exit code alone is not read, sincetestthat::test_file()exits with 0 when a test fails unless it is givenstop_on_failure = TRUE. Use a reporter that prints the summary line, such as the default,"progress"or"check": under"summary"no such line is printed and the status is"error".stop_on_failure = TRUEmakes the exit code agree with the result.
A program stopped at timeout seconds is "timeout", with the
degradation "command_timeout", and a program that cannot be found is
"error". No warning from system2() reaches the caller.
With held = FALSE the step returns its input, a list, with the result
in $check, so the next step can read it (a revise step, say). With
held = TRUE the next step must not see it: the step returns its input
unchanged, records the status as the signal "command.<step>" (see
hr_signal()), and attaches the result to the output as
attr(output, "hr_held"), a list named by step, which a scorer from
hr_scorer() reads. A held step must come last: hr_chain() refuses a
step that is not held after one that is, with class hr_held_not_last.
In a benchmark the step notes the run in the trace as a local step,
"step.command", with hashes of argv and of the output, never the output
itself, and hidden_env, the number of variables hidden from the program
(never their names or values). It sends no request, so it costs nothing.
Credentials
The child process inherits the session's environment variables except the
ones that look like credentials, which are unset for the call and put back
after it. A variable is hidden when its name ends in _API_KEY, _TOKEN,
_SECRET, _PASSWORD, _PAT or _SECRET_ACCESS_KEY (in any case), when
it is one of the provider variables OPENAI_API_KEY, ANTHROPIC_API_KEY,
GOOGLE_API_KEY, GEMINI_API_KEY, AZURE_OPENAI_API_KEY, GITHUB_PAT,
GITHUB_TOKEN, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN and
HF_TOKEN, when its name matches a regular expression in
hr_options(command_hide_env =), or when its value is 8 characters or
longer and is one of the values of hr_options(api_headers =) or the key
set with options(harnessr.api_key =), such as a gateway key read with
Sys.getenv(); a shorter value, such as "true", is no credential and
hides nothing.
A program that needs one of these variables cannot read it; pass what it
needs another way. hr_options(command_temp_home = TRUE) also runs the
program with HOME (and USERPROFILE on Windows) set to an empty folder
made for the call and removed after it, so it does not read your
configuration files and stored logins.
With hr_options(mask_secrets = TRUE) the stdout and stderr kept in
the result have key-shaped strings replaced with <masked>, and the step
records how many in the trace as masked. The values of the hidden
variables are masked as well. The hash of the output is taken before
masking. The masked output is what the step passes on in $check, so a
later step that builds a prompt from it sends the masked text.
hr_options() lists the shapes masked. Neither setting
confines the program: it still runs with your permissions and can read
your files.
See also
hr_step(), hr_chain(), hr_scorer()
Other harness:
gr_arm(),
gr_condensation_check(),
gr_qa_reference(),
gr_reverse_text(),
gr_scorer_answer(),
hr_arm(),
hr_benchmark(),
hr_chain(),
hr_contrast(),
hr_drift_check(),
hr_effort_check(),
hr_effort_escalate(),
hr_effort_event(),
hr_effort_log(),
hr_mdd(),
hr_modules(),
hr_reference(),
hr_register(),
hr_report(),
hr_scorer(),
hr_set(),
hr_settings(),
hr_skill(),
hr_step(),
hr_sweep(),
hr_sweep_check()
Examples
rscript <- file.path(R.home("bin"), "Rscript")
check <- hr_step_command("check", c(rscript, "-e", "quit(status = 0)"))
check(list(draft = "x"), list())$check$status
#> [1] "passed"
# The command can depend on the step's input.
f <- tempfile(fileext = ".R")
writeLines("stopifnot(1 + 1 == 2)", f)
run_file <- hr_step_command("run", function(input, ctx) c(rscript, input$file))
run_file(list(file = f), list())$check$status
#> [1] "passed"
# Held back from later steps, for the scorer.
ref <- hr_reference(list(q1 = "f"), labels = "passed", cluster = "a", split = "test")
chain <- hr_chain(
draft = hr_step("draft", function(input, ctx) list(item = input)),
check = hr_step_command("check", c(rscript, "-e", "quit(status = 1)"),
held = TRUE),
name = "held")
passes <- hr_scorer(function(item, output, label) {
s <- attr(output, "hr_held")$check$status
list(correct = identical(s, label), outcome = s, failure_kind = NA)
}, name = "passes")
bench <- hr_benchmark(chain, ref, runs = 5, client = NULL, scorer = passes)
table(bench$outcome)
#>
#> failed
#> 5
unlink(f)