Jobs and Auto-Discovery¶
Functualize automatically discovers job functions from Python modules and registers them as CLI commands. This guide explains how the discovery mechanism works, what makes a function eligible for registration, and how to organize jobs into grouped sub-commands.
How auto-discovery works¶
When you create a FunctualizeApp, the job_sources parameter tells the framework where to look for job modules:
from functualize.app import FunctualizeApp, JobSources
app = FunctualizeApp(
name="my-app",
job_sources=JobSources(directories=["src/my_app/jobs"]), # (1)!
)
- You can pass multiple directories. Each one is scanned independently.
The discovery process uses Python's pkgutil.iter_modules to scan each directory. For every Python module found (.py files), the framework:
- Skips sub-packages — only top-level modules in the directory are scanned, not nested packages
- Imports the module — loads it so functions and module-level variables can be inspected
- Inspects all attributes — checks each public attribute against the eligibility criteria
- Registers qualifying functions — adds them as Click CLI commands
Import failures are non-fatal
If a module fails to import (syntax error, missing dependency, etc.), a warning is logged and discovery continues with the remaining modules. Your application won't crash because of one broken job file.
Job names are canonical¶
A job's name is derived from its function name and normalized to lowercase-hyphenated form — the spelling command-line tools conventionally use, and the one Click itself defaults to:
| You write | The job is | You run |
|---|---|---|
def data_sync() |
data-sync |
func data-sync |
def buildWheel() |
build-wheel |
func build-wheel |
def job_aadž() |
job-aadž |
func job_aadž or func job-aadž |
JOB_GROUP = "data_ops" + def run_etl() |
data-ops.run-etl |
func data-ops run-etl |
Names are NFKC-normalized first, as Python normalizes identifiers when it
parses them, then lowercased and hyphenated. This also lets written Unicode
names resolve through rc.invoke, rc.invoke_parallel and Deps.
Python identifiers cannot contain hyphens and command names conventionally do, so without a single canonical form the same job has two spellings and every consumer picks one.
Typing the Python spelling still works. func data_sync,
rc.invoke("data_sync") and Deps("data_sync") all reach data-sync. That is
normalization, not aliasing: there is one name, and you cannot miss it by
writing it the way Python spells it. func --help always shows the canonical
form.
Two places keep the underscored spelling for good reasons:
- Environment variables —
DATA_SYNC_BATCH_SIZE, because no shell can export a name containing a hyphen. The variable is spelled from the registered name:job-aadžusesJOB_AADŽ_FIELD. - Config sections —
[data_sync]and[data-sync]are both read, so an existing config file keeps working. Non-ASCII TOML keys require quotes, for example["job_aadž"]forjob-aadž.
Two functions whose names normalize to the same job (build_wheel and
buildWheel) do not both register. One of them runs, chosen the same way on
every run, and every surface that shows the command says so — see
Duplicate command detection.
Identity and command path¶
A job has two spellings, and each has one job:
| Example | Used by | |
|---|---|---|
| Identity — dotted | deploy.web.run |
rc.invoke(...), deps=, FromJob, [aliases] targets, state keys, MCP tool names, every --json listing |
| Command path — spaced | deploy web run |
what you type after func or your app's entry point, and every hint, usage line, help page and TUI row that shows you a command |
The shell walks groups one word at a time. func deploy web run runs the job;
func deploy.web.run is refused with a hint naming the spaced command, because
a dot in a shell token never means "go into this group".
A dot that belongs to the job's own name is not a group boundary. A job
registered as tools.lint with no group is the single command func tools.lint,
and Job(name="pack", group="ci") is func ci pack. Code that prints commands
for people reads the path from the descriptor rather than rebuilding it from the
name:
from functualize.types import CommandPath
descriptor = app.get_job("deploy.web.run")
descriptor.name # "deploy.web.run" — the identity
str(descriptor.command_path) # "deploy web run" — what a person types
CommandPath.of(None, "tools.lint").segments # ("tools.lint",) — one word
func --help lists your top-level jobs and groups beside builtin, and
func <group> lists what is under a group.
Function eligibility criteria¶
Not every function in a job module becomes a CLI command. A function is registered only if all of the following are true:
| Criterion | Description |
|---|---|
| Callable | The attribute must be callable |
| Is a function | Must be an actual function (inspect.isfunction). A class registers only in the job-class form — @job on a pydantic.BaseModel subclass (Job classes) |
| No underscore prefix | The name must not start with _ — underscore-prefixed functions are treated as private |
| Defined in the module | The function must be defined in the scanned module, not imported from elsewhere |
The "defined in module" check prevents imported helper functions from accidentally becoming CLI commands. Only functions whose source module matches the scanned module are registered.
# jobs/data_tasks.py
from some_library import helper_function # (1)!
JOB_GROUP = "data"
def export(): # (2)!
"""Export data to CSV."""
print("Exporting...")
def _validate_row(row): # (3)!
"""Internal validation helper."""
return row is not None
class DataProcessor: # (4)!
"""Not registered — it's a plain class, not a job class."""
pass
helper_functionis not registered — it's imported from another module.exportis registered — it's a public function defined in this module._validate_rowis not registered — it starts with an underscore.DataProcessoris not registered — a class registers only as a job class:@jobon apydantic.BaseModelsubclass, where each public method is a job (Job classes). A bare class with no@jobcontributes nothing, exactly as before.
Which parameters are inputs¶
Discovery decides, once per parameter, what kind of input it is, and every
surface reads that one decision: func <job> --help, shell completion, the
inline TUI and its SmartBar, func builtin info schema, the MCP tool, the HTTP
route and the Lambda handler. A cached boot decides exactly as a fresh one, and
so does a module written with from __future__ import annotations.
- A caller input is supplied on the command line and on the wire. A
required one with a plain scalar type is a positional argument; one with a
default is a
--flag; aboolis a--flag/--no-flagpair,Falsewhen it is declared without a default. A list or atuple[T, ...]is a repeatable flag, and the job receives the container it declared. - A wire-only input (a
dict, aset, a fixedtuple[A, B]) has no command-line spelling. MCP, HTTP and Lambda accept it; on a command line a required one is a usage error naming those surfaces. - A config field is a field of the job's config model, resolved from the command line, the environment and config files (see JobConfig with Pydantic).
- A parameter supplied by the framework (a capability such as
Log, aFromJobinput, a class a plugin provides) is published on no surface. A call that names it is refused. - A refused parameter keeps its job listed, so
--helpstill describes it, but discovery reports the reason on every boot and every call to the job is refused with that reason and exit code 2.
An unannotated parameter is text: it binds as p: str does on every surface.
| Declaration | Kind | func <job> |
MCP, HTTP, Lambda, info schema |
The job receives |
|---|---|---|---|---|
p: str |
caller input | positional P, required |
p, required |
str |
p: str = "d" |
caller input | --p |
p |
str |
p: Optional[str] = None |
caller input | --p |
p |
str |
p: bool |
caller input | --p |
p |
bool |
p: list[str] |
caller input | --p, repeatable, required |
p, required |
list[str] |
p: Optional[list[str]] = None |
caller input | --p, repeatable |
p |
list[str] |
p: tuple[str, ...] |
caller input | --p, repeatable, required |
p, required |
tuple[str, ...] |
p: Annotated[list[str], Arg()] |
caller input | positional P, variadic, required |
p, required |
list[str] |
p: tuple[str, int] |
caller input, wire only | — | p, required |
tuple |
p: dict[str, str] |
caller input, wire only | — | p, required |
dict |
f: int, a field of a config model p: Cfg |
config field | --f, required |
f, required |
int |
p: Log | None = None |
supplied by the framework | — | — | Log |
p: ProvWidget, a class a plugin provides |
supplied by the framework | — | — | ProvWidget |
p: Annotated[ProvWidget, Provide("nope")], no provider under that qualifier |
refused at discovery | — | — | — (every call is refused) |
p: Widget, a class nothing provides |
refused at discovery | — | — | — (every call is refused) |
p: str | int |
refused at discovery | — | — | — (every call is refused) |
p: Secret[str] |
refused at discovery | — | — | — (every call is refused) |
p: str, / |
refused at discovery | — | — | — (every call is refused) |
p: Annotated[str, Arg(), Option()] |
refused at discovery | — | — | — (every call is refused) |
Domain capabilities: ai: AI and tasks: Tasks¶
AI (from functualize-ai) and Tasks (from functualize-tasks) are classes
a plugin provides, so they follow the supplied by the framework row above.
They have no flag, they appear in no schema, and the job receives an instance.
The plugin that supplies them is the domain's provider:
from functualize_ai import AI
from functualize_tasks import Tasks
def ask(ai: AI) -> str:
return ai.complete("Summarise the release notes.")
def plan(tasks: Tasks) -> int:
tasks.add("write the migration")
return len(tasks.list())
This works the same through func file.py ask, through func ask from the
project directory, and through a FunctualizeApp, on cold and warm boots.
- One provider installed (
functualize-ai-pydantic,functualize-tasks-local): it is selected and supplies the parameter. No configuration is needed. - Several installed: choose one with
[ai] provider = "<name>"or[tasks] provider = "<name>". With none chosen, nothing is selected and the parameter is refused with a reason naming that key. - None installed, or a configured name that is not installed: the parameter is refused at discovery. The reason names the package to install (or the configured name and its key), and every call exits nonzero with it.
See Domain SDKs for how providers are selected.
When the built-in filters cannot describe your jobs¶
The require_* settings describe a module by its filename, its imports, a
marker, or a decorator. That covers most projects. It does not cover a host
whose jobs are chosen by a rule only that host knows — a naming convention of
your own, a registry, a generated tree. (Methods on a class are not such a
rule: that is the job-class form, described in Job classes.)
DiscoveryConfig.pre_filter takes your own predicate, asked before the module
is imported:
from pathlib import Path
from functualize.app import FunctualizeApp, JobSources
from functualize.app.config import DiscoveryConfig
class HasJobSuffix:
"""Admit a module only when its name ends in `_tasks` or `_ops`."""
def should_import(self, source_file: Path) -> bool:
return source_file.stem.endswith(("_tasks", "_ops"))
def fingerprint(self) -> str:
return "has-job-suffix:v1"
app = FunctualizeApp(
"myapp",
job_sources=JobSources(directories=["./jobs"]),
discovery_config=DiscoveryConfig(pre_filter=HasJobSuffix()),
)
Two things about it are worth knowing before you write one.
It composes; it does not replace¶
Your filter is ANDed onto the stack the require_* settings build. Setting
both means a module has to satisfy both:
DiscoveryConfig(
require_file_prefix="job_", # filename starts with job_
pre_filter=HasJobSuffix(), # ...and ends with _tasks or _ops
)
It runs last, after the cheap filename checks, so an expensive predicate sees the fewest possible files.
fingerprint() is not optional¶
The discovery cache stores which files your filter rejected and replays those decisions on the next boot, so a scan does not re-read a file it already ruled out. That is only safe while the cache can tell that your filter still behaves the same way.
fingerprint() returns a stable string that you change when the filter's logic
changes. Return the same value across processes for the same behaviour, and a
new value when you edit the predicate:
Identity cannot be used instead. An object's str() carries its memory
address, so a digest built from the object itself would differ on every boot —
invalidating the cache on every run while appearing to work. A filter with no
fingerprint() is refused with a TypeError rather than silently cached
wrong.
Forgetting to bump it gives you a stale cache: the same contract, and the same
failure mode, as editing a require_* setting without invalidating.
lazy=False applies no discovery filter at all
JobSources(lazy=False) opts out of the cached provider and, with it, out
of filtering entirely — not just pre_filter, but exclude_patterns and
every require_* setting too. This is a known defect, not a design; the
default (lazy=True) is the filtered path.
The protocol¶
ModulePreFilter is exported from functualize.plugin if you want to declare
the type. There is nothing to inherit — it is satisfied structurally:
from functualize.plugin import ModulePreFilter
def check(candidate: ModulePreFilter) -> bool:
return isinstance(candidate, ModulePreFilter) # both methods required
JOB_GROUP and sub-command grouping¶
The JOB_GROUP module-level variable controls how functions are organized in the CLI hierarchy.
With JOB_GROUP (grouped)¶
When a module defines JOB_GROUP, all qualifying functions in that module are registered under a Click sub-command group named after the JOB_GROUP value:
# jobs/reporting.py
JOB_GROUP = "report" # (1)!
def generate(format: str = "pdf"):
"""Generate a report."""
print(f"Generating {format} report...")
def send(recipient: str = "team@example.com"):
"""Send the latest report."""
print(f"Sending report to {recipient}...")
- All public functions in this module become sub-commands under
my-app report.
This produces the following CLI structure:
Without JOB_GROUP (top-level)¶
When a module does not define JOB_GROUP, its qualifying functions are registered as top-level commands on the main application:
# jobs/health.py
# No JOB_GROUP defined
def ping():
"""Check if the service is alive."""
print("pong")
This registers ping directly on the app:
Multiple modules sharing the same JOB_GROUP¶
Multiple job modules can share the same JOB_GROUP value. Their functions are all registered under the same sub-command group:
Both export and load appear under the data sub-command group:
Organizing large projects
Splitting related functions across multiple files while sharing a JOB_GROUP keeps individual modules focused and manageable, while presenting a unified command group to users.
Duplicate command detection¶
When a command name is already registered at the same level (either within the same group or at the top level):
This can happen when:
- Two modules with the same
JOB_GROUPboth define a function with the same name - Two modules without
JOB_GROUPboth define a function with the same name
One claimant runs: the last by source file, then Python name, so the same function wins on every run and on cold and warm discovery alike. The other is not registered, and the collision is a warning rather than an error, so the rest of your jobs keep working.
The warning is shown wherever the command is: one line on stderr before the
job runs, under the command's row in func <group>, in func builtin info
(text and JSON), and on the TUI's job browser row and pre-flight panel. It names
the command path, both functions with their files, and which one runs:
JobNameCollision: tools status: status_ (/srv/app/jobs/tools_more.py) runs; status (/srv/app/jobs/tools.py) was dropped. Rename one job.
In func builtin info jobs --json the surviving job's row keeps its dotted
name and gains a collisions array. Rename one of the functions.
Minimal job file example¶
Here's a complete, minimal job file with annotations showing which elements are required for discovery:
"""Sample job module demonstrating auto-discovery requirements."""
JOB_GROUP = "sample" # (1)!
def run(target: str, dry_run: bool = False): # (2)!
"""Execute the sample job. # (3)!
Args:
target: The target resource to process.
dry_run: If True, simulate without making changes.
"""
print(f"Starting sample job with target: {target}")
if dry_run:
print("Dry run mode — skipping actual processing")
print("Job completed successfully")
JOB_GROUP— Groups this module's functions under thesamplesub-command. Remove this line to register functions at the top level instead.- Public function — Must be a function or a job class, must not start with
_, and must be defined in this module. A class registers only in the job-class form —@jobon apydantic.BaseModelsubclass, with each public method becoming one job. Parameters with type annotations become Click CLI options automatically. - Docstring — Used as the command's help text in
--helpoutput.
This produces:
$ my-app sample run --help
Usage: my-app sample run [OPTIONS]
Execute the sample job.
Options:
--target TEXT The target resource to process. [required]
--dry-run / --no-dry-run
If True, simulate without making changes. [default: no-dry-run]
--help Show this message and exit.
Job Metadata¶
@job attaches structured metadata to job functions. It is available via JobDescriptor.declaration and useful for AI orchestrators, schema exporters, and documentation generators.
from functualize.job import job
from functualize.job import RunContext
JOB_GROUP = "deploy"
@job(
extra_description="Deploy the application to the specified environment",
category="deployment",
examples=["deploy --env production", "deploy --env staging --dry-run"],
tags=["deploy", "infrastructure", "production"],
)
def run(rc: RunContext) -> None:
"""Deploy the application."""
...
Identity and description parameters¶
| Parameter | Type | Description |
|---|---|---|
group |
str \| None |
Overrides the module-level JOB_GROUP |
extra_description |
str \| None |
Description beyond the docstring summary, for AI/LLM consumption |
category |
str \| None |
Grouping category for job organization |
examples |
tuple[str, ...] \| list[str] |
Usage examples |
tags |
tuple[str, ...] \| list[str] |
Searchable tags; also drives func mcp serve --include-tags |
visibility |
"external" \| "internal" |
"internal" hides the job from MCP. Defaults to "external" |
config_section |
str \| None |
Config section this job reads |
@job also carries the operational contract — deps, cache, guards, exec — as self-validating value objects. See the @job API reference.
The declaration is stored as a frozen JobDeclaration on the function's __functualize_job__ attribute and incorporated into the JobDescriptor during registration. It round-trips through the discovery cache, so warm boot has it without importing the module.
Replaces @job_metadata
The @job_metadata decorator has been removed. Its ai_description argument is now extra_description; category, examples, tags and visibility keep their names.
Composable with other decorators
@job can be combined with any other decorators in any order. It does not wrap the function — it only attaches an attribute, so decorated is original always holds.
JobDescriptor Retention¶
After registration, every job's JobDescriptor is retained and accessible for runtime introspection:
# From application code
descriptors = app.job_registry.get_descriptors() # All registered descriptors
descriptor = app.job_registry.get_descriptor("data-sync") # Single descriptor by name
print(descriptor.name) # "data-sync"
print(descriptor.group) # "data" or None
print(descriptor.config_schema) # Pydantic model class or None
print(descriptor.metadata) # JobMetadataAnnotation or None
From within a job, use rc.discovery.get_job_schema(job_name) to introspect sibling jobs:
def orchestrator(rc: RunContext) -> None:
schema = rc.discovery.get_job_schema("validate-data")
rc.log(f"Job has {len(schema.config_schema.model_fields)} config fields")
This enables patterns like dynamic orchestration, job discovery UIs, and AI-driven job selection.
Next steps¶
- JobConfig with Pydantic — Add typed, validated configuration to your jobs with automatic CLI option generation
- RunContext Lifecycle — Inject runtime context with lifecycle hooks into your job functions
- Configuration System — Understand how config values are resolved from multiple sources