featherweight.jobs¶
jobs ¶
Jobs: a Job is a declared class, run in your process by run_job or on a worker by name, by the same code (execute_job).
A Job has a name, a version, an input model and an output model (Pydantic), and async run(ctx, input). A plain agent can
be a job too (as_job). A job is what a client submits to a queue and a worker runs (see docs/specs/submitted-jobs.md).
JobTimeout ¶
Bases: TimeoutError
A job ran for longer than its timeout. It is the error type that the job fails with.
JobContext ¶
Bases: BaseModel
What a job gets when it runs: the runner and its own id. run and run_many run agents in this job, so that they
share its trace and its resume.
Job ¶
Bases: ABC
A job, declared as a class, like an agent. Subclass it, set name, version, input_model and output_model, and write
run. A worker is given the classes it can run, and runs one by its name.
The checks are made when the class is defined: a missing or odd name or version, or a model that is not Pydantic, or a
timeout that is not a positive number or None, is an error then, not when a job arrives. A class that has not written
run cannot be built.
timeout is how many seconds one execution of the job may run (default 600; None for no limit). A job that exceeds it is
cancelled and fails with error type JobTimeout, in a worker and in run_job alike. It is not part of job_hash.
job_hash
classmethod
¶
job_hash() -> str
SHA-256 of the declaration: it changes when the name, the version or either schema does.
run
abstractmethod
async
¶
run(ctx: JobContext, input: Any) -> Any
Do the job: run agents with ctx.run, and return an instance of output_model.
RunFailed ¶
Bases: RuntimeError
An agent that was run as a job failed (its attempts were used up). The message is the agent's error.
InvalidJobOutput ¶
Bases: Exception
A job returned something that is not an instance of its output model.
job ¶
job(fn: Callable[..., Awaitable[Any]]) -> type[Job]
job(
*,
name: str | None = None,
version: str = "1",
timeout: float | None = DEFAULT_JOB_TIMEOUT,
) -> Callable[[Callable[..., Awaitable[Any]]], type[Job]]
job(
fn: Callable[..., Awaitable[Any]] | None = None,
/,
*,
name: str | None = None,
version: str = "1",
timeout: float | None = DEFAULT_JOB_TIMEOUT,
) -> Any
Make a Job class from an async function: async def summarise(ctx: JobContext, input: In) -> Out. The name is the
function's (or name=), the version "1" (or version=), and the input and output models are the annotations of input and
the return value. timeout= is the seconds one execution may run (default 600, None for no limit). The function becomes the class, usable wherever a Job is; the function itself is Summarise.function.
@job
async def summarise(ctx: JobContext, input: SummariseInput) -> SummariseOutput: ...
@job(name="summarise", version="2", timeout=120)
async def summarise(ctx, input: SummariseInput) -> SummariseOutput: ...
as_job ¶
A Job class as it is, or a plain agent class as a job: its name, its input model, its output model, and agent_hash
as the job hash. A streaming agent cannot be a job (it streams to a person).
execute_job
async
¶
execute_job(
runner: AgentRunner,
job: type[Job],
job_id: str,
job_input: BaseModel,
results: ResultStore | None,
*,
on_cancel: Callable[[], Awaitable[None]] | None = None,
trace_context: Mapping[str, str] = {},
owner: JobOwner | None = None,
lost: Callable[[], bool] | None = None,
identity: Any = None,
) -> JobOutcome[BaseModel]
Run a job that has been started (its row is RUNNING), keep its result and set its final status. This is the one function
that runs a job, for run_job (in your process) and for the worker, so that the two cannot differ: the same row, hashes,
events, result, and the same calls to the runner's observers.
A job that raises is a FAILED outcome, not an exception (the error type and hash go to the run log, the text to the log and
the outcome). A cancelled task sets CANCELLED (or runs on_cancel, which the worker uses to put a job back on the queue)
and re-raises the cancellation.
The observers' job scope wraps the whole run, so that an observer can make its span the current one while the pipeline
runs. trace_context is the carrier that came with the message (a worker), and is empty for run_job.
owner (who started the job) makes every write to the job conditional on it still being the owner, in the statement itself.
A write that finds the job taken from it raises JobLost and writes nothing: this function lets it through (the observers'
scope ends with error type JobLost, for tracing only) and the caller drops the job. A task that is cancelled because the
job was lost (lost() says so) writes nothing to the job either.
run_job
async
¶
run_job(
runner: AgentRunner,
job: type[Job] | type[AgentBase],
input: BaseModel,
*,
job_id: str | None = None,
resume_from: str | None = None,
resumable: bool = False,
trace_context: Mapping[str, str] = {},
identity: Any = None,
) -> JobOutcome[Any]
Run a job in your process: create it (or use job_id), start it, run it with execute_job (the code a worker runs it
with), and return the outcome. job is a Job class, or a plain (non-streaming) agent class; input must be an instance
of its input model (TypeError otherwise). The output is the job's output model, in outcome.result, and it is also written
to the runner's result store if the runner has one. A job that raises is a FAILED outcome, not an exception; a cancelled
task is marked CANCELLED and the cancellation is re-raised.
resumable=True keeps the outputs of the job's steps, so that a later job can resume it. It is decided here, before the job
runs: a job that did not keep them cannot be resumed. resume_from is the id of an earlier, resumable job: the new job
records it, and a step that the earlier job already finished, with the same agent and input, is answered from the kept result
instead of calling the model. resume_from and resumable cannot be combined with job_id: create the job with
db.create_job(name, resume_from=...) and pass that.
To run in the background and give the caller the id first: create the job yourself with db.create_job, then
asyncio.create_task(run_job(runner, job, input, job_id=job_id)). A job that was cancelled (or started) before it could
start is not run: the outcome has the status it has and no result.
trace_context is the carrier of the caller's trace (W3C traceparent), for a runner that starts a job as a background job:
the job's span then joins that trace. It is empty otherwise.
identity is who the job is for. It is checked against the runner's identity_type (a TypeError when it is missing, of the
wrong type, or given when no type is declared), the job reads it as ctx.identity, and every agent it runs has it.