Runs and permissions
A run is one piece of work of the program, such as one command. A workflow opens it. A permission limits what the tools of a model get in a run.
| A run holds | What it is |
|---|---|
| A grant | The permissions of the run. It comes from the permission group the workflow names. When it names none, the grant gives every permission. |
| Values | The fields of the input of each workflow that started in the run. |
| Run components | One instance of each, built the first time a tool, a flow step or another run component of the run needs it. |
What a run is
Section titled “What a run is”- Open or join. A workflow opens a run when none is open. A workflow that runs inside an open run joins it: as a step of another workflow, or called from a flow step.
- Values. A workflow that joins a run may add values. It may not change one: a key the run already has, with another value, throws an error that names the key.
- Run components. What a run component takes with
@Useis built first. Needs that come at the same time share one build. @Usefields. Before a flow step’sexecuteand before a tool call, the kit makes ready everything the class takes with@Use. A field read only looks the instance up.
How code reads the open run:
| Where | How |
|---|---|
| In a flow step | this.run, for example this.run.grant |
| Other code | RunScope.find(), which gives undefined outside a run, or RunScope.current(), which throws there |
Run components
Section titled “Run components”A run component is a capability with scope: "run" or a class marked with @RunService(name). It is the place for state of one run.
LineCount.ts
import { OnFlowStart, RunService, Use } from "@kerpetensh/harness-kit";import { TextFile } from "./TextFile";
@RunService("notes:line-count")export class LineCount { @Use(TextFile) private readonly file!: TextFile;
private counted = 0;
public get lines(): number { return this.counted; }
@OnFlowStart({ type: "run" }) public async count(): Promise<void> { this.counted = (await this.file.lines(1, Number.MAX_SAFE_INTEGER)).length; }}A run component takes capabilities and other run components with @Use, and is taken by others with @Use only. Components has the whole table.
A class and a subclass registered under the same name are one component of a run: whichever is asked for, the run builds the registered class once. A test uses this to replace a component; see Testing.
Run values
Section titled “Run values”@RunValue("path")private readonly path!: string;- The key is a field name of a workflow’s input.
- The type of the field is the type of the value. The compiler does not check it.
- A run without that value throws when the field is read. The error names the class, the field and the key.
- A run component reads the run that built it, also in its constructor and in its start hooks.
Every field of the input becomes a value, also the ones no component reads. In A first program the input holds path, llm and log, and the capability reads path.
Start hooks
Section titled “Start hooks”A run component has three hooks for work at its start.
| Hook | From | When it runs |
|---|---|---|
@OnStart() |
@asenajs/ioc |
When the run builds the component, inside the run. The run waits for it before anything reads the component. |
@OnFlowStart({ type: "run" }) |
the kit | Once in the run. When a workflow that reaches the component starts, the kit builds the component and waits for the hook before the first flow step. A component that is built later runs it right after its build. |
@OnFlowStart({ type: "workflow" }) |
the kit | Every time a workflow that reaches the component starts in the run. |
Which one to use:
| Setup | Hook |
|---|---|
| Can wait until the first need | @OnStart |
| Must be done before the first flow step, for example before a model is called | @OnFlowStart({ type: "run" }) |
The rules of a hook:
- The marked method takes no arguments and may return a promise.
- A hook that throws ends the workflow with its error before any flow step runs.
@OnFlowStartis for run components only. On any other class it stops the start of the container.
What a workflow reaches:
- what its own flow steps take with
@Useand@Inject, - the tools of a harness,
- what those take in turn.
A tool’s @Use whose permission the grant does not give is not followed. A workflow listed as a step reaches its own components when it starts.
When a build fails
Section titled “When a build fails”A run component whose build throws fails the whole run.
- The run keeps the first such error as
run.failure. - That error ends the harness runs of the run and its workflow, also when a flow step caught it or threw another error on its account.
- A workflow around the failed one ends with the same error.
Permissions
Section titled “Permissions”| Word | What it is |
|---|---|
| Permission | A string a capability declares: lowercase words joined by colons, like files:read. A word starts with a letter and holds letters, digits and hyphens. Another form throws when the class is defined. |
| Permission group | A named list of permissions. The group all is built in and gives every permission; a program cannot declare a group with that name. |
| Grant | What one run has, taken from one group. |
The program gives its groups to the application:
permissionGroups: new PermissionGroupSet({ offline: ["files:read"], online: ["files:read", "web:search"],}),A workflow names the group of the run it opens by overriding group. Here the input carries the name:
AnswerWorkflow.ts
import { type After, type OptionalGap, type PermissionGrant, Workflow, WorkflowBase } from "@kerpetensh/harness-kit";import type { AnswerInput } from "./answer-types";import { AnswerStep } from "./AnswerStep";
@Workflow({ name: "answer", description: "Answers a question about a text file.", steps: [AnswerStep] })export class AnswerWorkflow extends WorkflowBase<AnswerInput, After<typeof AnswerStep>, string> { protected override group(input: AnswerInput): string | undefined { return input.group; }
protected override admit(input: AnswerInput, _grant: PermissionGrant, gaps: readonly OptionalGap[]): void { for (const gap of gaps) { input.log.warn(`the group ${gap.group} does not give ${gap.permission}; ${gap.tool} runs without ${gap.capability}`); } }
protected finish(state: After<typeof AnswerStep>): string { return state.answer; }}groupreturningundefinedmeans the groupall.- A workflow that joins an open run does not name a group. It runs with the grant the run already has, and that grant is checked against the harnesses the joining workflow reaches.
- A name the program does not have ends the workflow with a
PermissionGroupError:
unknown permission group "nobody"; known groups: all, offline, onlineWhat a permission checks
Section titled “What a permission checks”Only a tool’s @Use is checked against the grant of the run. There are three checks, and all three are about tools.
| When | What is checked | A mistake |
|---|---|---|
| The container starts | The permissions that harnesses, tools and groups list; see What the start checks. | Stops the start with a WiringError. |
| A workflow starts | The grant is checked against the harnesses the workflow reaches. Then admit runs. |
A grant that misses a permission one of them cannot run without ends the workflow with a PermissionGroupError, before anything of the run is built. |
| A tool call | A capability whose permission the grant does not give is never built for the tool. | Reading a required use throws a PermissionError, which becomes the failed outcome of that tool call. |
The error of the second check names the group and each harness:
the permission group "web" cannot start this run:- harness answer needs the permission files:readA harness runs only in a run a workflow started, so the second check always comes before the first tool call. The one exception is a run opened by hand with test: true.
Optional uses
Section titled “Optional uses”A tool may take a capability it can work without. The field then reads undefined in a run whose grant does not give the permission.
LookupTool.ts
import { Tool, ToolError, Use } from "@kerpetensh/harness-kit";import type { z } from "zod";import { lookupInput } from "./answer-schemas";import { TextFile } from "./TextFile";import { WebSearch } from "./WebSearch";
@Tool({ name: "lookup", description: "Finds the lines of the file that hold the query. It may also add results from the web.", input: lookupInput,})export class LookupTool { @Use(TextFile) private readonly file!: TextFile;
@Use(WebSearch, { required: false }) private readonly web: WebSearch | undefined;
public async execute(input: z.infer<typeof lookupInput>): Promise<string[]> { const lines = await this.file.lines(1, 10_000); const found = lines.filter((line) => line.includes(input.query)); const online = this.web === undefined ? [] : await this.web.search(input.query);
if (found.length === 0 && online.length === 0) throw new ToolError("nothing found");
return [...found, ...online]; }}- The type of an optional field is the class
| undefined, and the field has no!. { required: false }has a meaning in a tool only.- The harness still lists the permission:
permissionsholds every permission its tools can use, optional uses included. - A tool description must not promise what an optional capability does, because the model may run without it.
With the group offline above:
- The run starts.
webreadsundefined.admitgets one gap: the harness, the tool, the capability, the permission and the group.
admit does nothing by default. A workflow overrides it to report the gaps, as AnswerWorkflow does, or to refuse a grant for a reason of its own by throwing.
What is not checked
Section titled “What is not checked”- A flow step’s
@Use. A flow step gets the capability whatever the grant is. - A run component’s
@Use, the uses of a capability included. A capability that a tool was given takes what it needs without a second check. @Inject, and any call from a flow step or a service to a capability.
So a permission limits what the tools of a model get in a run. It does not limit the program’s own code.
- A flow step that must refuse something for a grant reads
this.run.grantand decides itself, or its workflow overridesadmit. - A capability without a permission is fine for flow steps and run components. A tool cannot take it.
Adding a permission
Section titled “Adding a permission”- Declare it on the capability:
permission: "area:action". - List it in
permissionsof every harness whose tools take the capability. - Add it to the permission groups that should give it. A permission that a tool’s required use needs must be in a group, or a run with that group cannot start a workflow that reaches the harness.