docs · reference

Adapter contract and conformance kit#

The adapter contract is the agreement between Hekma and every adapter: what an adapter declares, what Hekma guarantees in return, and how the two stay compatible as both change. It is at version 1.0.0 in Hekma 0.11.0, and at 1.1.0 on Hekma's main branch (Unreleased: after 0.11.0; what changed). The conformance kit is a Rust library that registers your adapter with a real engine, exercises it, and returns a report you can assert on in your own tests.

The contract#

The contract lives in the hekma-adapter-api crate and has its own version, separate from Hekma's. A manifest adapter writes its terms in adapter.toml; a built-in kind declares the same things in code.

What an adapter declares#

DeclarationManifest sectionWhat Hekma does with it
Kind[adapter] kindShows it in list and stores it with the agent.
Start command[lifecycle.start]Copies it at registration and runs it at every start.
Capabilities per OS[capabilities]Allows, labels or refuses pause, resume and send.
Metering source[metering]Reads usage lines from the agent, or runs a metering proxy.
Config mapping[config]Delivers config keys as environment variables, flags or files at start.
Input channel[interaction]Recorded only; Hekma 0.11.0 always delivers input through stdin.
Readiness probe (Unreleased: after 0.11.0)[readiness]Holds the start in starting until the probe passes.
Liveness check (Unreleased: after 0.11.0)[liveness]Fails a running agent whose check keeps failing.
Exit-code classes (Unreleased: after 0.11.0)[exit_codes]Relaunches on a requested restart, settles a clean exit as stopped.
Pause and resume commands (Unreleased: after 0.11.0)[lifecycle.pause], [lifecycle.resume]Runs them instead of freezing the process.
Drain wait (Unreleased: after 0.11.0)[drain]Bounds the wait of hekma agent drain.

An adapter must declare at least one supported capability and a metering source, or registration is refused with nothing written.

What Hekma guarantees#

  • One launcher. Hekma starts every agent the same way, in its own process group (a job object on Windows), with the Agent Home as working directory and stdout and stderr captured to files.
  • Stop always ends the process. Stop is SIGTERM to the group, then SIGKILL after the timeout (on Windows, the job is ended after the timeout). No process from the agent's tree is left behind. Stop, pause and resume templates in a manifest are not run in contract 1.0. In contract 1.1.0 (Unreleased: after 0.11.0) a declared pause and resume pair is run instead of the signals (pause and resume commands); a stop template is still not run.
  • Capabilities are honoured as declared. A guaranteed pause suspends the process; a best-effort one is labelled as such on the state change; an unsupported one fails before anything changes. An agent without interaction support never gets a stdin pipe, so it cannot hang waiting for input.
  • An agent's own exit is a crash unless declared otherwise. Any exit while running is handed to the restart policy. In contract 1.1.0 (Unreleased: after 0.11.0) the adapter can declare which codes ask for a relaunch and which mean a clean finish ([exit_codes]).
  • Readiness and liveness are what you declare. In contract 1.0 an agent is ready once its process has survived 300 milliseconds, and alive while its process exists. In contract 1.1.0 (Unreleased: after 0.11.0) the adapter can declare a readiness probe and a liveness check. Neither finds the agent's address for you: both poll the loopback URL or file the manifest names, so fixing the port stays the adapter's job.
  • Usage is never double-counted. Each usage event is keyed by agent, run and sequence; a replay is dropped. Usage the adapter cannot report is left out, never recorded as zero.
  • Config is delivered only where declared. A config key reaches the agent only through a mapping the adapter declared, plus agent.* keys as environment variables. The memory folder path (memory.dir) is offered, never imposed.
  • Secrets stay masked. A secret: value reaches the agent in clear text and appears masked in every Hekma output.

What an adapter promises#

Some duties fall on the adapter author because only the real agent can show whether they hold. The conformance kit cannot test them; review and re-testing at each agent release do.

  • Report usage accurately: one usage line per model call, a sequence that increases within a run, and nothing at all for a call whose usage is unknown.
  • If the agent fills {env:VAR} placeholders in its own config and treats missing variables as empty, make sure each such variable is set before launch.
  • Map the agent's setting that turns off self-update, and say whether the agent has a higher-priority config layer (for example one managed by an organisation) that could override what Hekma passes.
  • Give each agent its own data and config folders inside its Agent Home (for example through HERMES_HOME, or XDG_DATA_HOME and XDG_CONFIG_HOME), and say when a setting you rely on is not documented by the agent's authors.

On Hekma's main branch (Unreleased: after 0.11.0), the engine reserves two more delivery keys for a launch's credentials, run.key and run.refresh_key (Run Keys): every agent receives its Run Key in HEKMA_RUN_KEY, an adapter may map either key to an env target only (a flag or file target is refused at registration), and a refresh credential is issued only when run.refresh_key is mapped. They are ordinary [config] mappings of two reserved keys; the contract version does not change. See the manifest's reserved credential keys.

Versioning#

The rule: a manifest loads only when its contract_version major number matches the contract major this Hekma speaks. Hekma 0.11.0 speaks 1.0.0, so any 1.x.y manifest passes the version check and 0.x or 2.x fails, naming both versions.

Matching majors is required, not sufficient. Within a major the contract only grows: new optional fields, new values. A newer 1.x manifest that uses a field this Hekma does not know still fails, because Hekma rejects unknown fields. Write the oldest 1.x version whose features you use.

For example, Hekma's main branch speaks 1.1.0 (Unreleased: after 0.11.0). A manifest with a [readiness] section loads there, and fails on Hekma 0.11.0 with the section named. Run hekma --version before you register a manifest that uses a 1.1.0 section with an older Hekma.

The value must be a full X.Y.Z version. Suffixes such as 1.0.0-rc.1 parse, and only the major is compared.

Hekma checks the version at registration. An agent that is already registered starts from the copy it made then, so editing contract_version afterwards has no effect until you remove and register the agent again.

Built-in kinds (acp, hermes, mock) carry no contract version; they ship with the engine and always match it.

Deprecation policy#

Within a contract major, a change that removes or alters something is announced in the release notes and these docs at least one minor release before it takes effect, and the removal happens only in the next major release. Additions need no notice. The same policy covers the CLI's JSON documents and exit codes. The public Rust API of hekma-adapter-api is checked for breaking changes against the 1.0.0 contract on every change to Hekma.

One deprecation is in effect: the hermes kind, in favour of --kind acp with Hermes Agent's hermes-acp command. It keeps working and will not be removed before a major release. See Supported agents.

The deprecation also waited for a way to run gateway-shaped agents without a built-in kind. That way is on Hekma's main branch (Unreleased: after 0.11.0): a gateway registers as a manifest adapter pointing at its own program, declaring a readiness probe, a heartbeat, its exit codes, a pause and resume pair and a drain, as in the service-mode example, and the conformance kit tests each of those. This does not remove the hermes kind; removal still waits for a major release.

What changed in 1.1.0#

Unreleased: after 0.11.0. Contract 1.1.0 is on Hekma's main branch and ships in the next release; Hekma 0.11.0 speaks 1.0.0.

Contract 1.1.0 adds five optional declarations to the manifest, all within major 1, so every 1.0.0 manifest loads unchanged:

DeclarationWhat it adds
[readiness]One probe (http-get, file-fresh or log-contains) that must pass before running. A probe that times out fails the agent with readiness-timeout, and the restart policy is not consulted.
[liveness]One check (heartbeat_file or http_get) that must not fail for longer than max_age_ms. A check that does fails the agent with unresponsive, and the restart policy applies.
[exit_codes]restart_requested codes relaunch the agent without counting toward the crash limit; clean codes settle it as stopped.
[lifecycle.pause] and [lifecycle.resume]A command pair run instead of SIGSTOP and SIGCONT, on every OS.
[drain]The bounded wait behind hekma agent drain: pause, wait for an idle file, then stop.

The engine also resets an agent's crash count when it crashes after at least 30 seconds of uninterrupted running. That is engine behaviour, not part of the contract, and applies to every adapter.

The Rust crate hekma-adapter-api carries the new types at 0.3.0. A program that reads Hekma's events or --json output from another version should expect the new state-change causes (readiness-timeout, unresponsive, restart-requested, clean-exit, pause-cooperative, resume-cooperative and drain-stopped): an older reader fails on a cause it does not know.

The conformance kit#

The kit is the hekma-conformance crate. It is not on crates.io, so add it from Git, pinned to the v0.11.0 release commit:

[dev-dependencies]
hekma-conformance = { git = "https://github.com/Ktesio/hekma", rev = "3471f0ef933167c881640bd840ee97321e483dad" }

Pin the commit rather than a branch, so the report format cannot change under you; move the pin when you choose to. The kit at the v0.11.0 commit tests contract 1.0.0; to test the 1.1.0 sections (Unreleased: after 0.11.0), pin a commit on Hekma's main branch instead. The kit pulls in the engine, so the first build compiles Hekma (Rust 1.96.1 or newer).

Run it#

Call the kit from an ordinary test. run_conformance registers your adapter with a fresh engine in a temporary state directory, runs all eight sections (twelve on Hekma's main branch, Unreleased: after 0.11.0), and returns a report:

use std::path::PathBuf;

use hekma_conformance::{run_conformance, TckAdapter};

#[test]
fn my_adapter_is_conformant() {
    let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("my-adapter");
    let report = run_conformance(&TckAdapter::Manifest(dir));
    assert!(report.is_conformant(), "failures: {:?}", report.failures());
}

TckAdapter::Manifest(dir) takes the directory that holds adapter.toml. TckAdapter::Native("hermes".into()) tests a built-in kind instead. run_mock_conformance(&dir) is a shorthand for the manifest case.

The kit starts your agent for real, several times, so the manifest's start command must work on the machine running the tests. A failed section never stops the run: every section reports.

The kit's own test agent is a small program from the kit crate, fake_agent. The kit looks for it next to your test binary, in target/debug/ (or target/release/). Build it there before running your tests, from a checkout of Hekma at the same commit:

git clone https://github.com/Ktesio/hekma /tmp/hekma-kit
git -C /tmp/hekma-kit checkout 3471f0ef933167c881640bd840ee97321e483dad
CARGO_TARGET_DIR=/tmp/hekma-kit-build cargo build --manifest-path /tmp/hekma-kit/Cargo.toml \
  -p hekma-conformance --bin fake_agent
cp /tmp/hekma-kit-build/debug/fake_agent target/debug/

Add --release to the build and copy from and to release/ if you run your tests with --release. Without the helper in place, the kit tries to build it itself, which fails from outside Hekma's own workspace in 0.11.0, and the sections that need it fail naming the missing binary.

Configure a run#

run_conformance_with takes a TckConfig for CI environments that need a different timeout, their own state directory, or a subset of sections. The defaults match run_conformance.

use std::path::PathBuf;
use std::time::Duration;

use hekma_conformance::{
    run_conformance_with, SectionSelection, TckAdapter, TckConfig, TckSection,
};

#[test]
fn lifecycle_and_config_only() {
    let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("my-adapter");
    let config = TckConfig {
        section_timeout: Duration::from_secs(10),
        sections: SectionSelection::Only(vec![TckSection::Lifecycle, TckSection::ConfigMapping]),
        ..TckConfig::default()
    };
    let report = run_conformance_with(&TckAdapter::Manifest(dir), &config);
    assert!(report.is_conformant(), "failures: {:?}", report.failures());
}
FieldDefaultMeaning
section_timeout30 sHow long each wait inside a section may take. Lower it for a fast CI runner, at the cost of flaky failures on a slow one.
state_roota new temporary directoryUse this directory as the engine's state directory.
sectionsSectionSelection::AllSectionSelection::Only(vec![...]) runs a subset; the report lists only those, in the usual order. An empty list is reported as a failure under the id selection.

A state_root you supply must be empty or not exist yet, and must serve one run only. The kit registers agents under fixed names and writes files at fixed paths, so a reused directory could pass a section on a previous run's files. A directory that is not empty, or cannot be created, fails the run before any section starts, with the reason. The kit leaves the directory's contents for you to inspect.

Sections#

Section idWhat it provesNot applicable when
capability_edgesThe capability declaration Hekma stored matches the one the adapter carries.never
lifecycleYour agent goes from start to running to stopped with the expected state changes. A test agent the kit writes, which exits with code 7, lands failed with a crash cause.never
drain (Unreleased: after 0.11.0)Your agent's drain runs its pause command, sees the idle file and completes within its bound (completed: true); a test agent that never goes idle runs out its bound and is still stopped (completed: false).The manifest has no [drain].
pausePause and resume behave as declared: a guaranteed pause really freezes a process; a best-effort one carries the best-effort labels. On Hekma's main branch, a declared pause and resume pair is shown to run, with its pause-cooperative and resume-cooperative causes, on every OS and level (Unreleased: after 0.11.0).pause is unsupported on this OS, or guaranteed on Windows, where Hekma cannot suspend a process (unless a command pair is declared).
readiness (Unreleased: after 0.11.0)Your agent reaches running through its probe; a test agent that never becomes ready lands failed with readiness-timeout and is not restarted.The manifest has no [readiness].
liveness (Unreleased: after 0.11.0)Your agent stays running past its window; a test agent whose heartbeat never beats lands failed with unresponsive.The manifest has no [liveness].
exit_classes (Unreleased: after 0.11.0)A test agent exiting with a restart_requested code is relaunched without its crash count rising; one exiting with a clean code lands stopped with clean-exit and no restart.The manifest has no [exit_codes].
config_mappingEvery env mapping in your [config] reaches your agent's environment, and an agent.* key arrives as its own variable. Uses the --dump convention below.The manifest has no [config], or maps only to flags and files; or the adapter is a built-in kind.
metering_self_reportedThree usage batches land as exactly three ledger rows, a replayed batch adds nothing, and fleet totals equal the ledger.The adapter declares engine-observed.
metering_engine_observedThree model calls through the metering proxy land exactly in the ledger.The adapter declares self-reported.
memoryAttach, delivery of the memory folder path through the declared mapping, and detach. With the --dump convention and an env mapping, delivery is proven on your agent itself.The adapter does not map memory.dir.
interactionsend reaches a running agent when interaction is supported, and fails at once when it is declared unsupported.The adapter does not declare interaction for this OS.

The sections run in the order of the table: drain comes before pause because the drain's pause command is the first to run on your agent. In the four sections added on Hekma's main branch, the failing cases always run on test agents declared like yours, never by breaking or exiting your agent, and a declaration the kit cannot read back fails the section with the reason. The kit's own gateway test agent declares all five service-mode sections, in the shape of the service-mode example.

capability_edges, lifecycle, pause and config_mapping run your adapter itself, as do the passing cases of drain, readiness and liveness. The metering sections, memory and interaction run small test agents the kit writes, declared the same way as yours where it matters, to prove the engine does its part; a failure there never touches your agent. The interaction section always tests stdin, whatever [interaction] channel you declare.

The --dump convention#

To prove config delivery, the kit needs to see your agent's environment. It looks for --dump <path> in your [lifecycle.start] args:

[lifecycle.start]
exec = "/usr/local/bin/my-agent"
args = ["--dump", "config-dump.txt"]

When started with --dump PATH, your agent (or a wrapper script around it) should write its environment to PATH, one env=NAME=VALUE line per variable, soon after start. A relative path is relative to the Agent Home. The kit sets each mapped key to a test value, starts the agent, and waits for the matching lines, such as env=MODEL=tck-model-value and env=TCK_PROBE=verbatim-1. A manifest that has env mappings but no --dump argument fails config_mapping with a reason that says so.

You can keep a test-only copy of the manifest that adds --dump, and point the kit at that copy.

The report#

ConformanceReport is plain data that serializes to JSON:

{
  "schema_version": 1,
  "contract_version": "1.0.0",
  "adapter_kind": "my-adapter",
  "sections": [
    { "section": "capability_edges", "result": { "status": "pass" } },
    { "section": "lifecycle", "result": { "status": "pass" } },
    { "section": "metering_engine_observed", "result": { "status": "not_applicable", "reason": "..." } },
    { "section": "interaction", "result": { "status": "fail", "reason": "..." } }
  ]
}
ItemMeaning
schema_versionThe report format, currently 1. Raised only for a breaking change; new fields can appear without it.
contract_versionThe contract version the engine speaks: 1.0.0 in 0.11.0, 1.1.0 on Hekma's main branch (Unreleased: after 0.11.0). A test that pins the literal needs updating.
adapter_kindThe kind under test.
sectionsOne entry per section run, in the order of the table above.
result.statuspass, fail with the first failure's reason, or not_applicable with a reason derived from your declaration.
is_conformant()true when no section failed and at least one ran.
failures()The failed sections as (id, reason) pairs, for assertion messages.
section(id)One section's result, for asserting on a specific section.

A failure before any section can run (an unusable state directory, a manifest that does not register) marks every selected section fail, with a reason that names the stage that failed.

A conformant report shows that the declared, testable parts of the contract hold. It says nothing about the promises only the real agent can keep.